Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Smarter debouncing
export function debounce (fn, wait = 1) {
let timeout
return function (...args) {
clearTimeout(timeout)
timeout = setTimeout(() => fn.call(this, ...args), wait)
}
}
function debounce (fn, wait) {
var timeout
return function () {
clearTimeout(timeout)
timeout = setTimeout(function () {
fn.apply(this, arguments)
}, (wait || 1))
}
}
window.addEventListener('resize', debounce(function () {
}, 500))
@kutyel

This comment has been minimized.

Copy link

kutyel commented Jun 14, 2017

What about this?

export function debounce(fn, wait = 1) {
  let timeout;
  return function (...args) {
    clearTimeout(timeout);
    timeout = setTimeout(() => fn.call(this, ...args), wait);
  }
}

I think it looks more ES2015 :)

@vincentorback

This comment has been minimized.

Copy link
Owner Author

vincentorback commented May 26, 2019

@kutyel yummy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.