Last active
July 15, 2023 15:12
-
-
Save fr-ser/ded7690b245223094cd876069456ed6c to your computer and use it in GitHub Desktop.
Typed debounce function wtih TypeScript
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
export function debounce<F extends Function>(func:F, wait:number):F { | |
let timeoutID:number; | |
if (!Number.isInteger(wait)) { | |
console.warn("Called debounce without a valid number") | |
wait = 300; | |
} | |
// conversion through any necessary as it wont satisfy criteria otherwise | |
return <any>function(this:any, ...args: any[]) { | |
clearTimeout(timeoutID); | |
const context = this; | |
timeoutID = window.setTimeout(function() { | |
func.apply(context, args); | |
}, wait); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good. Thanks a lot :)