Skip to content

Instantly share code, notes, and snippets.

@mrkitan
Created June 25, 2020 15:11
Show Gist options
  • Select an option

  • Save mrkitan/9d0052a1ecc68ce939825ee4f4741bde to your computer and use it in GitHub Desktop.

Select an option

Save mrkitan/9d0052a1ecc68ce939825ee4f4741bde to your computer and use it in GitHub Desktop.
debounce TypeScript
const debounce = (fn: (...params: any[]) => any, delay: number, immed: boolean = false) => {
let timer: number | undefined = undefined;
return function(this: any, ...args: any[]) {
if (timer === undefined && immed) {
fn.apply(this, args);
}
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
return timer;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment