Created
July 30, 2020 04:52
-
-
Save sudikrt/a88499d6954ff31139073dfd84f6cee1 to your computer and use it in GitHub Desktop.
Debouncing_Example
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
<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> |
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
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