24 lines
539 B
TypeScript
24 lines
539 B
TypeScript
export function throttle<T extends (...args: unknown[]) => void>(
|
|
func: T,
|
|
wait: number
|
|
): (...args: Parameters<T>) => void {
|
|
let inThrottle: boolean = false;
|
|
let lastArgs: Parameters<T> | null = null;
|
|
|
|
return function (...args: Parameters<T>) {
|
|
if (!inThrottle) {
|
|
func(...args);
|
|
inThrottle = true;
|
|
|
|
setTimeout(() => {
|
|
inThrottle = false;
|
|
if (lastArgs) {
|
|
func(...lastArgs);
|
|
lastArgs = null;
|
|
}
|
|
}, wait);
|
|
} else {
|
|
lastArgs = args;
|
|
}
|
|
};
|
|
} |