2 * jQuery.timers - Timer abstractions for jQuery
3 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
4 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
7 * @author Blair Mitchelmore
13 everyTime: function(interval, label, fn, times) {
14 return this.each(function() {
15 jQuery.timer.add(this, interval, label, fn, times);
18 oneTime: function(interval, label, fn) {
19 return this.each(function() {
20 jQuery.timer.add(this, interval, label, fn, 1);
23 stopTime: function(label, fn) {
24 return this.each(function() {
25 jQuery.timer.remove(this, label, fn);
34 dataKey: "jQuery.timer",
35 regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
37 // Yeah this is major overkill...
46 timeParse: function(value) {
47 if (value == undefined || value == null)
49 var result = this.regex.exec(jQuery.trim(value.toString()));
51 var num = parseFloat(result[1]);
52 var mult = this.powers[result[2]] || 1;
58 add: function(element, interval, label, fn, times) {
61 if (jQuery.isFunction(label)) {
68 interval = jQuery.timer.timeParse(interval);
70 if (typeof interval != 'number' || isNaN(interval) || interval < 0)
73 if (typeof times != 'number' || isNaN(times) || times < 0)
78 var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
83 fn.timerID = fn.timerID || this.guid++;
85 var handler = function() {
86 if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
87 jQuery.timer.remove(element, label, fn);
90 handler.timerID = fn.timerID;
92 if (!timers[label][fn.timerID])
93 timers[label][fn.timerID] = window.setInterval(handler,interval);
95 this.global.push( element );
98 remove: function(element, label, fn) {
99 var timers = jQuery.data(element, this.dataKey), ret;
104 for ( label in timers )
105 this.remove(element, label, fn);
106 } else if ( timers[label] ) {
109 window.clearInterval(timers[label][fn.timerID]);
110 delete timers[label][fn.timerID];
113 for ( var fn in timers[label] ) {
114 window.clearInterval(timers[label][fn]);
115 delete timers[label][fn];
119 for ( ret in timers[label] ) break;
122 delete timers[label];
126 for ( ret in timers ) break;
128 jQuery.removeData(element, this.dataKey);
134 jQuery(window).bind("unload", function() {
135 jQuery.each(jQuery.timer.global, function(index, item) {
136 jQuery.timer.remove(item);