Skip to content

Instantly share code, notes, and snippets.

@joelbarbosa
Last active March 11, 2020 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelbarbosa/6f2ea93d3444a51ce16b46124732c05b to your computer and use it in GitHub Desktop.
Save joelbarbosa/6f2ea93d3444a51ce16b46124732c05b to your computer and use it in GitHub Desktop.
const debounce = (fn, wait, immediate) => {
let timeout;
return () => {
const later = () => {
timeout = null;
if (!immediate) fn.apply(this)
}
const callNow = immediate && !timeout;
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) fn.apply(this)
}
}
const myDebounce = debounce(() => {
console.log('call me')
}, 500)
const input = document.querySelector('input');
input.addEventListener('input', myDebounce)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment