DevHero
Derived
@ccrystle·Handcrafted

I made the debounce faster.

Forked from @ccrystle's demo and tweaked it.

+1 −1 vs original
javascript
function debounce(fn, ms) {
  let t;
  const wrapped = (...args) => {
    clearTimeout(t);
    t = setTimeout(() => fn(...args), ms);
  };
  wrapped.cancel = () => clearTimeout(t);
  return wrapped;
}

const out = document.getElementById("out");
const run = debounce((v) => { out.textContent = "debounced: " + v; }, 1);
document.getElementById("q").addEventListener("input", (e) => run(e.target.value));
Live demo

Riffs on this win (4)

Comments (0)

Sign in to comment.