Skip to content

Instantly share code, notes, and snippets.

@kanakiyajay
Created September 16, 2014 12:30
Show Gist options
  • Save kanakiyajay/0a677c5b48ee4cf8a361 to your computer and use it in GitHub Desktop.
Save kanakiyajay/0a677c5b48ee4cf8a361 to your computer and use it in GitHub Desktop.
Postpone/Debounce a function important for showing autocomplete search results and saving ajax requests.
function debounce (delay, callback) {
var timeout = null;
return function (){
if (timeout) {
clearTimeout(timeout);
}
var context = this;
var args = arguments;
timeout = setTimeout(function(){
callback.apply(context, args);
timeout = null;
}, delay)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment