Files
4597fb1e-122f-4da6-9517-c98…/src/utils/throttle.ts
2025-12-25 11:42:30 +00: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;
}
};
}