mirror of
https://github.com/revoltchat/revite.git
synced 2025-01-03 19:24:44 -05:00
16 lines
459 B
TypeScript
16 lines
459 B
TypeScript
|
export function debounce(cb: Function, duration: number) {
|
||
|
// Store the timer variable.
|
||
|
let timer: number;
|
||
|
// 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);
|
||
|
};
|
||
|
}
|