dev4web
1/3/2017 - 8:34 PM

Smarter debouncing

Smarter debouncing

// vanilla
window.addEventListener('resize', debounce(function () {

}, 500));

// es6
window.addEventListener('resize', debounce(() => {

}, 500));
function debounce(fn, wait) {
  var timeout;
  return function () {
    clearTimeout(timeout);
    timeout = setTimeout(function () {
      fn.apply(this, arguments)
    }, (wait || 1));
  }
}
export function debounce(fn, wait) {
  let timeout;
  return function () {
    clearTimeout(timeout);
    timeout = setTimeout(() => fn.apply(this, arguments), (wait || 1));
  }
}