Debounce & Throttling

Debounce

window.addEventListener('input', debounce(function(e) {
    console.log(e.target.value);
}, 200));



const debounce =(fun, delay)=>{
  let timer;
  return function(){
    const args = agrguments;
    clearTimeout(timer);
    
    timer = setTimeout(()=> {
      func.apply(this,args);
    },delay)
  }
}

Throttling

const throttle=(func,delay)=>{
  let timer;
  return ()=> {
      const args = arguments;
      if(timer){
        return;
      }
    
      timer = setTimeout(()=> {
        func.apply(this.args);
        timer = undefined;
      },delay);
  }
}