I was following a tutorial on throttling and this was the code the guy used. However I am unable to understand how the code functions. Shouldn't last be set to zero every time we call throttle?
const throttle = (fn, delay ) => {
let last = 0
return (...args) => {
const now = new Date().getTime();
if(now-last<delay){
return;
}
last=now; //how does this work - wouldn't last be set to zero the next time the function runs?
return fn(...args)
}
}
document.getElementById('myid').addEventListener('click',throttle(()=>{
console.log('you clicked me')
},5000))