Skip to content

Instantly share code, notes, and snippets.

@InfamousStarFox
Last active April 3, 2021 00:07
Show Gist options
  • Save InfamousStarFox/3c7a2a8089ce551e0153230ebe7bdee8 to your computer and use it in GitHub Desktop.
Save InfamousStarFox/3c7a2a8089ce551e0153230ebe7bdee8 to your computer and use it in GitHub Desktop.
Typescript Debounce
// Debounce calls a function after N amount of time passes since its last call
// Example, debounce(callback, 100) called every 10ms. Callback will never run.
// Example, debounce(callback, 100) called 30 times in 10ms, then not called again. Callback will run 100ms after the last call.
export function debounce(callback:Function, time:number){
let timeout: ReturnType<typeof setTimeout>;
return function(){
clearTimeout(timeout);
timeout = setTimeout(function(this:any){
callback.apply(this, arguments);
}, time);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment