Why does this debounce helper never invoke the callback after repeated input?

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

You used setInterval/clearInterval, which keeps creating a repeating timer, but debounce wants a one-shot setTimeout/clearTimeout.

Arthur :smiling_face_with_sunglasses: