Spaces:
Paused
Paused
File size: 968 Bytes
d69879c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/**
* Custom throttle function that allows the first call to go through immediately
* and then limits subsequent calls.
* @param func - The function to throttle.
* @param limit - The minimum time between function calls in milliseconds.
* @returns A throttled version of the function.
*/
export function throttle<T extends (...args: any[]) => any>(func: T, limit: number): T {
let lastCall = 0;
let timeoutId: NodeJS.Timer | null = null;
return function (this: any, ...args: Parameters<T>) {
const context = this;
const now = Date.now();
if (now - lastCall >= limit) {
if (timeoutId !== null) {
clearTimeout(timeoutId);
timeoutId = null;
}
lastCall = now;
return func.apply(context, args);
} else if (!timeoutId) {
timeoutId = setTimeout(() => {
lastCall = Date.now();
timeoutId = null;
func.apply(context, args);
}, limit - (now - lastCall));
}
} as T;
}
|