Files
Nikolay Pecheniev 39b6468dbf Initial commit
2026-02-10 19:04:07 +02:00

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;
}
};
}