Last active
March 11, 2020 09:59
-
-
Save joelbarbosa/6f2ea93d3444a51ce16b46124732c05b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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