revite/src/lib/debounce.ts
2021-06-19 22:39:30 +01:00

15 lines
467 B
TypeScript

export function debounce(cb: Function, duration: number) {
// Store the timer variable.
let timer: NodeJS.Timeout;
// This function is given to React.
return (...args: any[]) => {
// Get rid of the old timer.
clearTimeout(timer);
// Set a new timer.
timer = setTimeout(() => {
// Instead calling the new function.
// (with the newer data)
cb(...args);
}, duration);
};
}