Source: https://www.freecodecamp.org/news/javascript-debounce-example/
function debounce(func: Function, timeout = 300) {
let applyAgain = false;
let timer: number;
return (...args: any) => {
if (!timer) {
func.apply(this, args);
} else {
applyAgain = true;
}
clearTimeout(timer);
timer = setTimeout(() => {
timer = undefined;
if (applyAgain) {
func.apply(this, args);
applyAgain = false;
}
}, timeout);
};
}
function debounce(func: Function, timeout = 300) {
let applyAgain = false;
let timer: number;
return (...args: any) => {
if (!timer) {
func.apply(this, args);
} else {
applyAgain = true;
}
clearTimeout(timer);
timer = setTimeout(() => {
timer = undefined;
if (applyAgain) {
func.apply(this, args);
applyAgain = false;
}
}, timeout);
};
}
function debounce(func: Function, timeout = 300) { let applyAgain = false; let timer: number; return (...args: any) => { if (!timer) { func.apply(this, args); } else { applyAgain = true; } clearTimeout(timer); timer = setTimeout(() => { timer = undefined; if (applyAgain) { func.apply(this, args); applyAgain = false; } }, timeout); }; }
503600cookie-checkTypescript / javascript minimal debounce