Files
b83b7ef5-3eba-4a9d-b6f3-79a…/src/utils/throttle.ts
2025-12-26 15:08:57 +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;
}
};
}