Skip to content

Instantly share code, notes, and snippets.

@influxweb
Forked from vincentorback/debounce-es2015.js
Created January 3, 2017 20:34
Show Gist options
  • Select an option

  • Save influxweb/54cb7b888900f1510412df6191ffb7c8 to your computer and use it in GitHub Desktop.

Select an option

Save influxweb/54cb7b888900f1510412df6191ffb7c8 to your computer and use it in GitHub Desktop.
Smarter debouncing
export function debounce(fn, wait) {
let timeout;
return function () {
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, arguments), (wait || 1));
}
}
function debounce(fn, wait) {
var timeout;
return function () {
clearTimeout(timeout);
timeout = setTimeout(function () {
fn.apply(this, arguments)
}, (wait || 1));
}
}
// vanilla
window.addEventListener('resize', debounce(function () {
}, 500));
// es6
window.addEventListener('resize', debounce(() => {
}, 500));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment