Skip to content

Instantly share code, notes, and snippets.

@shad
Created December 15, 2010 03:53
Show Gist options
  • Save shad/741605 to your computer and use it in GitHub Desktop.
Save shad/741605 to your computer and use it in GitHub Desktop.
Sometimes, it's useful to quickly set/clear a timeout that's attached to an element.
/*
* setTimeout
*
* Sometimes, we want to set a timeout and associate the timeout with
* an element. This is essential for operations that open menus, popups
* flyovers, etc.
*
* Shad Reynolds
* http://shad.github.com
*/
$.fn.extend({
setTimeout: function (fn, time) {
this.each(function () {
var elem = $(this), timeout = elem.data('timeout');
clearTimeout(timeout);
if (time) {
timeout = setTimeout( function () {
fn.call(elem);
elem.data('timeout', null);
}, time);
elem.data('timeout', timeout);
}else{
elem.data('timeout', null);
}
});
},
clearTimeout: function () {
$(this).setTimeout();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment