Debounce
example shows the debounce effect in JS. Mainly used in search with API call
by DineshAngappa
HTML
<input type="text" name="search" onkeypress="betterFunction()" />
JavaScript
let flag= true, timer;
let counter = 0;
let getData = function() {
console.log('Fetching data::', counter++);
}
const debounce = function(fn, d) {
let timer;
return function() {
console.log(timer);
let context = this,
args = arguments;
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(context, arguments);
}, d)
}
}
const betterFunction = debounce(getData, 300)