Skip to content

Instantly share code, notes, and snippets.

@crazyoptimist
Created July 5, 2022 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazyoptimist/99ce995a7971f3be795fda973df68cca to your computer and use it in GitHub Desktop.
Save crazyoptimist/99ce995a7971f3be795fda973df68cca to your computer and use it in GitHub Desktop.
Debounce Wrapper in Typescript
export function debounce<T extends unknown[], U>(
callback: (...args: T) => U,
wait: number,
): (...args: T) => void {
let timer: NodeJS.Timeout | undefined = undefined
return (...args: T): void => {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => callback(...args), wait)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment