This is a standard debounced function that works well (tested...)
function debounce(func, wait = 300, delay = false) {
let waitingClock;
return function () {
const context = this;
const args = arguments;
const immediateCall = !waitingClock && !delay;
clearTimeout(waitingClock);
waitingClock = setTimeout(function () {
waitingClock = null;
if(!immediateCall)
func.apply(context, args);
}, wait);
if(immediateCall)
func.apply(context, args);
}
}
const log = function() {
console.count("Logging something ");
}
We generally do this:
const safeLog = debounce(log, 300, false);
and it works ... 100 %
But I have seen that if I do this,
const safeLog = debounce(debounce(log, 300, false));
const safeLog = debounce(debounce(debounce(log, 300, false)));
const safeLog = debounce(debounce(debounce(debounce(log, 300, false))));
it still works same like above. So is it safe to conclude debounce is an idempotent function ?
I need some insight as to why ?