Throttle
Time interval it will be clalled used for scrolling/ resizing of window scenaio
by DineshAngappa
HTML
<input type="text" name="search" onkeypress="betterFunction()" />
JavaScript
let counter = 0;
const getData = () => {
console.log('Fetching data::', counter++);
}
const throtle = function(fn, d) {
let flag = true;
return function() {
let context = this,
args = arguments;
if(flag){
fn.apply(context, args);
flag = false;
setTimeout(function(){
flag = true;
}, d);
}
}
}
const betterFunction = throtle(getData, 3000);