Files
2b81905c-b5c8-4114-97a0-5d7…/src/utils/throttle.ts
Nikolay Pecheniev e54f266164 Initial commit
2026-01-28 16:03:46 +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;
}
};
}