222: I made the debounce faster.
Forked from @ccrystle/posts/1421af3546654fd49c1fd3b4a76a65fa's demo and tweaked it.
javascript+1 −1 vs original
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; }, 222);
document.getElementById("q").addEventListener("input", (e) => run(e.target.value));Live demo