Created
June 9, 2022 10:59
-
-
Save rehasantiago/a3498eb6a1188c012770d96cc2e10d86 to your computer and use it in GitHub Desktop.
How long to debounce text input - https://readosapien.com/how-long-to-debounce-text-input/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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