18 lines
473 B
TypeScript
18 lines
473 B
TypeScript
export function debounce<T extends (...args: any[]) => void>(
|
|
func: T,
|
|
wait: number
|
|
): (...args: Parameters<T>) => void {
|
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
return function (this: any, ...args: Parameters<T>) {
|
|
const context = this;
|
|
const later = () => {
|
|
timeout = undefined;
|
|
func.apply(context, args);
|
|
};
|
|
if (timeout !== undefined) {
|
|
clearTimeout(timeout);
|
|
}
|
|
timeout = setTimeout(later, wait);
|
|
};
|
|
}
|