Skip to content

Instantly share code, notes, and snippets.

@kylealwyn
Last active January 18, 2017 22:20
Show Gist options
  • Save kylealwyn/7f57576e95cc92909237847b7ffbb788 to your computer and use it in GitHub Desktop.
Save kylealwyn/7f57576e95cc92909237847b7ffbb788 to your computer and use it in GitHub Desktop.
ES6 versions of popular curried functions
function debounce(fn, delay) {
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
}
}
function throttle(fn, delay) {
let waiting = false;
return function(...args) {
if (waiting) { return; }
fn.apply(this, args);
waiting = true;
setTimeout(() => {
waiting = false;
}, delay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment