const throttle = (callback: (...args: unknown[]) => unknown, delay: number) => {
let timer: NodeJS.Timeout;
let isWaiting = true;
return (...args: unknown[]) => {
if (isWaiting) {
callback(...args);
isWaiting = false;
setTimeout(() => {
isWaiting = true;
}, waitingTime);
}
};
};
export default throttle;
const throttle = <TArgs extends unknown[]>(
callback: (...args: TArgs) => unknown,
waitingTime: number,
) => {
let isWaiting = true;
return (...args: TArgs) => {
if (isWaiting) {
callback(...args);
isWaiting = false;
setTimeout(() => {
isWaiting = true;
}, waitingTime);
}
};
};
export default throttle;
Documentation - Do's and Don'ts
TypeScript Generic Rest Parameters and Tuple Types in Practice