Skip to content

Instantly share code, notes, and snippets.

@C-Rodg
Created November 4, 2016 01:35
Show Gist options
  • Save C-Rodg/320619bedf8d0a5787ba3d83aab771c2 to your computer and use it in GitHub Desktop.
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.
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