Skip to content

Instantly share code, notes, and snippets.

@rehasantiago
Created June 9, 2022 10:59
Show Gist options
  • Save rehasantiago/a3498eb6a1188c012770d96cc2e10d86 to your computer and use it in GitHub Desktop.
Save rehasantiago/a3498eb6a1188c012770d96cc2e10d86 to your computer and use it in GitHub Desktop.
<script>
// Debouncing in Javascript
let counter = 0;
const getData = () => {
// calls an API and gets Data
console.log("Fetching Data ..", counter++);
}
const debounce = function (fn, d) {
let timer;
return function () {
let context = this,
args = arguments;
clearTimeout(timer);
timer = setTimeout(() => {
getData.apply(context, arguments);
}, d);
}
}
const betterFunction = debounce(getData, 300);
</script>
<input type="text" onkeyup="betterFunction()"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment