Created
June 21, 2013 22:45
-
-
Save weisjohn/5834885 to your computer and use it in GitHub Desktop.
an example of variable delays in event handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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