Skip to content

Instantly share code, notes, and snippets.

@Stichoza
Created May 21, 2015 23:14
Show Gist options
  • Save Stichoza/1670f35d7a8facf0e0bc to your computer and use it in GitHub Desktop.
Save Stichoza/1670f35d7a8facf0e0bc to your computer and use it in GitHub Desktop.
Delayed Callback
var timeout, callback;
callback = function(event) {
console.log("Hey!");
}
button.addEventListener('click', function(event) {
clearTimeout(timeout);
timeout = setTimeout(function() {
callback(event);
}, 300);
});
@safareli
Copy link

var delay = function(interval,fn){
    var timeout;
    return function(){
      var args = arguments;
      clearTimeout(timeout);
      timeout = setTimeout(function() {
          fn.apply(undefined,arguments);
      }, interval);
    }
};

button.addEventListener('click', delay(300, function(event) {
    console.log("Hey!");
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment