Created
June 21, 2019 20:25
-
-
Save zenius/3cc28e2628203132e9ebac46d6e338ee to your computer and use it in GitHub Desktop.
debounce in javascript
This file contains hidden or 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 = function() { | |
console.log('fetching data ...' + counter++); | |
}; | |
const debounce = function(fn, delay) { | |
let timer; | |
return function() { | |
let context = this, | |
args = arguments; | |
clearTimeout(timer); | |
timer = setTimeout(()=> { | |
fn.apply(this, arguments); | |
}, delay); | |
} | |
}; | |
const getDataAfterPause = debounce(getData, 500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment