DevHero
Derived
@ccrystle·Handcrafted

DIFF: I made the debounce faster.

Forked from @ccrystle's demo and tweaked it.

+2 −0 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;
}
//this is a comment 
//for the sake od showing a diff

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

Comments (0)

Sign in to comment.