blob: 4b685e75657d536d7727254f63c754774c8410a8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* Creates a debounced version of a function that delays execution until after
* the specified wait time has elapsed since the last invocation.
*/
export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number
): (...args: Parameters<T>) => void {
let timeoutId: ReturnType<typeof setTimeout> | null = null;
return function (...args: Parameters<T>) {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
func(...args);
timeoutId = null;
}, wait);
};
}
/**
* Clears a timeout if it exists
*/
export function clearDebounce(timeoutId: ReturnType<typeof setTimeout> | null) {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
}
|