22 lines
491 B
TypeScript
22 lines
491 B
TypeScript
|
|
/**
|
||
|
|
* Debounce function - delays execution until after wait time has elapsed
|
||
|
|
*/
|
||
|
|
export function debounce<T extends (...args: any[]) => any>(
|
||
|
|
func: T,
|
||
|
|
wait: number
|
||
|
|
): (...args: Parameters<T>) => void {
|
||
|
|
let timeout: NodeJS.Timeout | null = null;
|
||
|
|
|
||
|
|
return function executedFunction(...args: Parameters<T>) {
|
||
|
|
const later = () => {
|
||
|
|
timeout = null;
|
||
|
|
func(...args);
|
||
|
|
};
|
||
|
|
|
||
|
|
if (timeout) {
|
||
|
|
clearTimeout(timeout);
|
||
|
|
}
|
||
|
|
timeout = setTimeout(later, wait);
|
||
|
|
};
|
||
|
|
}
|