Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active August 27, 2015 18:42
Show Gist options
  • Save johntran/162ce9d19c08e492afa8 to your computer and use it in GitHub Desktop.
Save johntran/162ce9d19c08e492afa8 to your computer and use it in GitHub Desktop.
function debounce(callback, wait, immediate) {
var timeout;
return function() {
var context = this;
var args = arguments;
var delayedFire = function() {
timeout = null;
if (!immediate) callback.apply(context, args);
};
clearTimeout(timeout);
timeout = setTimeout(delayedFire, wait);
if (immediate && !timeout) callback.apply(context, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment