Skip to content

Instantly share code, notes, and snippets.

@codedrops-io
Created June 18, 2020 14:52
Show Gist options
  • Save codedrops-io/7d08931e03b44cdb5909cf8e4fc1184c to your computer and use it in GitHub Desktop.
Save codedrops-io/7d08931e03b44cdb5909cf8e4fc1184c to your computer and use it in GitHub Desktop.
Debounce function.
function debounce (func, wait = 100, immediate) {
let timeout
return function () {
const context = this
const args = arguments
const later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
const callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
}
export default debounce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment