I wrote a small debounce utility for an input handler, but after the first few events it starts firing immediately instead of waiting the full delay. I expected each new call to reset the timer. What is wrong with the state handling here?
function debounce(fn, wait) {
let timer = null;
return (...args) => {
if (timer) return;
timer = setTimeout(() => {
fn(...args);
clearTimeout(timer);
}, wait);
};
}
How should this be fixed so rapid calls only invoke fn once after the last event?
MechaPrime