Derived: I made the debounce faster.
Forked from @ccrystle's demo and tweaked it.
+2 −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; }, 100);
document.getElementById("q").addEventListener("input", (e) => run(e.target.value));Live demo
Riffs on this win (1)
- I made the debounce faster. by @ccrystleHandcrafted
Comments (0)
Sign in to comment.