Last active
April 3, 2021 00:07
-
-
Save InfamousStarFox/3c7a2a8089ce551e0153230ebe7bdee8 to your computer and use it in GitHub Desktop.
Typescript Debounce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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