Skip to content

Instantly share code, notes, and snippets.

@moiseshilario
Created March 6, 2018 12:51
Show Gist options
  • Save moiseshilario/1d729fc1076833dc40bb7ec7a393b905 to your computer and use it in GitHub Desktop.
Save moiseshilario/1d729fc1076833dc40bb7ec7a393b905 to your computer and use it in GitHub Desktop.
Debounce function
const debounce = (func, wait, immediate) => {
let timeout
return function() {
const context = this
const args = arguments
const later = () => {
timeout = null
if(!immediate) func.apply(context, args)
}
const callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if(callNow) func.apply(context, args)
}
}
// You can call the function like this:
// $(someElement).keyup(debounce(yourFunction, DEBOUNCE_TIME))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment