forked from M3-Academy/m3-academy-template-vtexio
27 lines
837 B
TypeScript
27 lines
837 B
TypeScript
export const throttle = <F extends (...args: any[]) => any>(
|
|
func: F,
|
|
waitFor: number
|
|
): any => {
|
|
const now = () => new Date().getTime();
|
|
const resetStartTime = () => (startTime = now());
|
|
let timeout: any;
|
|
let startTime: number = now() - waitFor;
|
|
|
|
return (...args: Parameters<F>): Promise<ReturnType<F>> =>
|
|
new Promise((resolve) => {
|
|
const timeLeft = startTime + waitFor - now();
|
|
if (timeout) {
|
|
clearTimeout(timeout);
|
|
}
|
|
if (startTime + waitFor <= now()) {
|
|
resetStartTime();
|
|
resolve(func(...args));
|
|
} else {
|
|
timeout = setTimeout(() => {
|
|
resetStartTime();
|
|
resolve(func(...args));
|
|
}, timeLeft);
|
|
}
|
|
});
|
|
};
|