Skip to content

Instantly share code, notes, and snippets.

@ottokruse
Last active April 21, 2022 13:48
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 ottokruse/f37e9ba9a8c8b669ed8e4e9877c84c05 to your computer and use it in GitHub Desktop.
Save ottokruse/f37e9ba9a8c8b669ed8e4e9877c84c05 to your computer and use it in GitHub Desktop.
TypeScript debounce that keeps argument types of debounced function
export function debounce<T extends (...args: any[]) => void>(
func: T,
timeout = 300
) {
let timer: ReturnType<typeof setTimeout>;
return (...args: Parameters<T>) => {
if (timer) clearTimeout(timer);
timer = setTimeout(() => func(...args), timeout);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment