Skip to content

Instantly share code, notes, and snippets.

@zenius
Created June 21, 2019 20:25
Show Gist options
  • Save zenius/3cc28e2628203132e9ebac46d6e338ee to your computer and use it in GitHub Desktop.
Save zenius/3cc28e2628203132e9ebac46d6e338ee to your computer and use it in GitHub Desktop.
debounce in javascript
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