Spaces:
Runtime error
Runtime error
File size: 513 Bytes
13095e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
export function throttle<T extends (...args: any[]) => any>(
func: T,
limit: number,
): T {
let lastFunc: ReturnType<typeof setTimeout>;
let lastRan: number;
return ((...args) => {
if (!lastRan) {
func(...args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastFunc = setTimeout(() => {
if (Date.now() - lastRan >= limit) {
func(...args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
}) as T;
}
|