Created
June 1, 2020 15:52
-
-
Save myWsq/9bafb50890c6fbec72571b20550d4efe to your computer and use it in GitHub Desktop.
Utils
This file contains 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<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