What is the significance of clearTimeout in debounce ?
Here is the code
const saveInput =(name)=>{
console.log('saveinput ',name);
}
const debounce = (fn,timeout = 3000)=>{
let timer ;
return (...args) =>{
clearTimeout(timer);
timer =setTimeout(()=>{
fn.apply(this,args);
},timeout)
}
}
const processChange = debounce(saveInput,2000);
processChange("foo");
processChange("foo");
processChange("foo");
processChange("foo");
processChange("foo");
Now i tried to comment out clearTimeout and this time foo gets printed five times and with clearTimeout ,foo gets printed just once .I need a clear explanation of how clearTimeout impcats .I understand that cleartimeout is used to clear time but i need an explanation that touches event loop etc.