Javascript Debounce
this Debounce for searching list
by Raj Shekhar
HTML
<input type="text" id="betterFunction"/>
<p>
Check console
</p>
JavaScript
const getData = () => console.log('Fetching Data......', counter++);
var counter = 0;
const doSomeMagic = function(fn, delay){
let timer;
return function(){
clearTimeout(timer);
let context = this, arg = arguments
timer = setTimeout(()=>{
fn.apply(context,arg);
},delay)
}
}
const betterFunction = doSomeMagic(getData, 200);
document.querySelector('#betterFunction').addEventListener('keyup',betterFunction)