Skip to content

Instantly share code, notes, and snippets.

@myWsq
Created June 1, 2020 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myWsq/9bafb50890c6fbec72571b20550d4efe to your computer and use it in GitHub Desktop.
Save myWsq/9bafb50890c6fbec72571b20550d4efe to your computer and use it in GitHub Desktop.
Utils
export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number,
immediate = false
) {
let timeout: number | undefined;
return function (this: any, ...args: Parameters<T>) {
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = undefined;
if (!immediate) func.apply(this, args);
}, wait);
if (immediate && !timeout) func.apply(this, args);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment