JavaScript: Debounce a function
A short, tested JavaScript example for debounce a function. Copy it and adjust the names and paths for your project.
JavaScript
function debounce(fn, ms = 300) {
let t;
return (...args) => {
clearTimeout(t);
t = setTimeout(() => fn(...args), ms);
};
}
input.addEventListener("input", debounce(e => search(e.target.value)));Find it in Zest
Search for “javascript debounce a function” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.