Skip to content

Instantly share code, notes, and snippets.

@rgabs
Created April 22, 2019 18:36
Show Gist options
  • Select an option

  • Save rgabs/1a79043da6022035ee5bb17cf63129ec to your computer and use it in GitHub Desktop.

Select an option

Save rgabs/1a79043da6022035ee5bb17cf63129ec to your computer and use it in GitHub Desktop.
simple throttle and debounce
const debounce = (fn, delay) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
fn(...args);
}, delay);
}
};
const throttle = (fn, delay) => {
let timer;
return (...args) => {
if(!timer) {
timer = setTimeout(() => {
fn(...args);
timer = null;
}, delay);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment