revite/src/lib/debounce.ts

16 lines
467 B
TypeScript
Raw Normal View History

2021-06-19 17:37:12 -04:00
export function debounce(cb: Function, duration: number) {
// Store the timer variable.
2021-06-19 17:39:30 -04:00
let timer: NodeJS.Timeout;
2021-06-19 17:37:12 -04:00
// 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);
};
}