Last active
March 19, 2016 01:38
-
-
Save sethmcleod/b16bac0440ae1143afb5 to your computer and use it in GitHub Desktop.
This limits the rate at which a function can fire, for optimized performance.
This file contains 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
function debounce(func, wait, immediate) { | |
let timeout; | |
return function() { | |
const context = this, 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); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment