Skip to content

Instantly share code, notes, and snippets.

@sudikrt
Created July 30, 2020 04:52
Show Gist options
  • Save sudikrt/a88499d6954ff31139073dfd84f6cee1 to your computer and use it in GitHub Desktop.
Save sudikrt/a88499d6954ff31139073dfd84f6cee1 to your computer and use it in GitHub Desktop.
Debouncing_Example
<html>
<head>
<title>Some Title</title>
</head>
<body>
<h1>This is hello world</h1>
<input type="text" onkeyup="betterFuntion()"/>
<script src="./index.js"></script>
</body>
</html>
let counter = 0;
const getData = () => {
//Call API to get data
console.log ('Fetching data -- :' + counter++);
}
const deBounce = function (fn, delay) {
let timer;
return function () {
let context = this, arg = arguments;
clearTimeout (timer);
timer = setTimeout (() => {
fn.apply (context, arg);
}, delay);
}
}
const betterFuntion = deBounce (getData, 300);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment