Created
December 15, 2010 03:53
-
-
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.
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
/* | |
* 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