Skip to content

Instantly share code, notes, and snippets.

@weisjohn
Created June 21, 2013 22:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weisjohn/5834885 to your computer and use it in GitHub Desktop.
Save weisjohn/5834885 to your computer and use it in GitHub Desktop.
an example of variable delays in event handlers
// how long should we wait for a function to fire?
var delay = 0;
// a simple way to wrap functions to be delayed
function postponed(fn) {
return function() {
setTimeout(fn, delay);
}
}
// modify the delay time
$(".delay").on('change', function(e) {
delay = $(this).val() * 1e3;
});
// register dynamic binders with postponed functions
var $elem = $('.some-element');
$elem.on('click', postponed(function() {
// do something
console.log('delayed by', delay, 'seconds');
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment