I’m writing a small debounce utility for a search box, but when I type quickly the callback seems to stop firing entirely instead of waiting for the last keystroke. I expected it to run once after the delay. What is wrong with this implementation?
function debounce(fn, wait) {
let timer = null;
return (...args) => {
if (timer) clearInterval(timer);
timer = setInterval(() => {
fn(...args);
}, wait);
};
}
const log = debounce(v => console.log(v), 200);
Sarah