I am interested in the \"debouncing\" function in javascript, written here : http://davidwalsh.name/javascript-debounce-function
Unfortunately the code is not explai
This is a variation which always fires the debounced function the first time it is called, with more descriptively named variables:
function debounce(fn, wait = 1000) {
let debounced = false;
let resetDebouncedTimeout = null;
return function(...args) {
if (!debounced) {
debounced = true;
fn(...args);
resetDebouncedTimeout = setTimeout(() => {
debounced = false;
}, wait);
} else {
clearTimeout(resetDebouncedTimeout);
resetDebouncedTimeout = setTimeout(() => {
debounced = false;
fn(...args);
}, wait);
}
}
};