Created
March 6, 2018 12:51
-
-
Save moiseshilario/1d729fc1076833dc40bb7ec7a393b905 to your computer and use it in GitHub Desktop.
Debounce function
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 = (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