The Node.js® Website
1type DebounceFunction<T = unknown> = (...args: Array<T>) => void;
2
3let timeoutId: NodeJS.Timeout;
4
5export const debounce =
6 <T extends DebounceFunction>(
7 func: T,
8 delay: number
9 ): ((...args: Parameters<T>) => void) =>
10 (...args: Parameters<T>) => {
11 clearTimeout(timeoutId);
12
13 timeoutId = setTimeout(() => {
14 func(...args);
15 }, delay);
16 };