starchat-ui / utils /data /throttle.ts
matthoffner's picture
Duplicate from matthoffner/chatbot
13095e0
raw
history blame contribute delete
513 Bytes
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;
}