Created
November 4, 2016 01:35
-
-
Save C-Rodg/320619bedf8d0a5787ba3d83aab771c2 to your computer and use it in GitHub Desktop.
A function that wraps another function and prevents it from repeatedly firing.
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
function debounce(func, immediate, wait) { | |
let timeout; | |
return function() { | |
let context = this, | |
args = arguments; | |
let callNow = immediate && !timeout; | |
clearTimeout(timeout); | |
timeout = setTimeout(function(){ | |
timeout = null; | |
if(!immediate) { | |
func.apply(context, args); | |
} | |
}, wait || 400); | |
if(callNow) { | |
func.apply(context, args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment