| [55] | 1 | jQuery.fn.extend({ |
|---|
| 2 | everyTime: function(interval, label, fn, times, belay) { |
|---|
| 3 | return this.each(function() { |
|---|
| 4 | jQuery.timer.add(this, interval, label, fn, times, belay); |
|---|
| 5 | }); |
|---|
| 6 | }, |
|---|
| 7 | oneTime: function(interval, label, fn) { |
|---|
| 8 | return this.each(function() { |
|---|
| 9 | jQuery.timer.add(this, interval, label, fn, 1); |
|---|
| 10 | }); |
|---|
| 11 | }, |
|---|
| 12 | stopTime: function(label, fn) { |
|---|
| 13 | return this.each(function() { |
|---|
| 14 | jQuery.timer.remove(this, label, fn); |
|---|
| 15 | }); |
|---|
| 16 | } |
|---|
| 17 | }); |
|---|
| 18 | |
|---|
| 19 | jQuery.extend({ |
|---|
| 20 | timer: { |
|---|
| 21 | guid: 1, |
|---|
| 22 | global: {}, |
|---|
| 23 | regex: /^([0-9]+)\s*(.*s)?$/, |
|---|
| 24 | powers: { |
|---|
| 25 | // Yeah this is major overkill... |
|---|
| 26 | 'ms': 1, |
|---|
| 27 | 'cs': 10, |
|---|
| 28 | 'ds': 100, |
|---|
| 29 | 's': 1000, |
|---|
| 30 | 'das': 10000, |
|---|
| 31 | 'hs': 100000, |
|---|
| 32 | 'ks': 1000000 |
|---|
| 33 | }, |
|---|
| 34 | timeParse: function(value) { |
|---|
| 35 | if (value == undefined || value == null) |
|---|
| 36 | return null; |
|---|
| 37 | var result = this.regex.exec(jQuery.trim(value.toString())); |
|---|
| 38 | if (result[2]) { |
|---|
| 39 | var num = parseInt(result[1], 10); |
|---|
| 40 | var mult = this.powers[result[2]] || 1; |
|---|
| 41 | return num * mult; |
|---|
| 42 | } else { |
|---|
| 43 | return value; |
|---|
| 44 | } |
|---|
| 45 | }, |
|---|
| 46 | add: function(element, interval, label, fn, times, belay) { |
|---|
| 47 | var counter = 0; |
|---|
| 48 | |
|---|
| 49 | if (jQuery.isFunction(label)) { |
|---|
| 50 | if (!times) |
|---|
| 51 | times = fn; |
|---|
| 52 | fn = label; |
|---|
| 53 | label = interval; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | interval = jQuery.timer.timeParse(interval); |
|---|
| 57 | |
|---|
| 58 | if (typeof interval != 'number' || isNaN(interval) || interval <= 0) |
|---|
| 59 | return; |
|---|
| 60 | |
|---|
| 61 | if (times && times.constructor != Number) { |
|---|
| 62 | belay = !!times; |
|---|
| 63 | times = 0; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | times = times || 0; |
|---|
| 67 | belay = belay || false; |
|---|
| 68 | |
|---|
| 69 | if (!element.$timers) |
|---|
| 70 | element.$timers = {}; |
|---|
| 71 | |
|---|
| 72 | if (!element.$timers[label]) |
|---|
| 73 | element.$timers[label] = {}; |
|---|
| 74 | |
|---|
| 75 | fn.$timerID = fn.$timerID || this.guid++; |
|---|
| 76 | |
|---|
| 77 | var handler = function() { |
|---|
| 78 | if (belay && this.inProgress) |
|---|
| 79 | return; |
|---|
| 80 | this.inProgress = true; |
|---|
| 81 | if ((++counter > times && times !== 0) || fn.call(element, counter) === false) |
|---|
| 82 | jQuery.timer.remove(element, label, fn); |
|---|
| 83 | this.inProgress = false; |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | handler.$timerID = fn.$timerID; |
|---|
| 87 | |
|---|
| 88 | if (!element.$timers[label][fn.$timerID]) |
|---|
| 89 | element.$timers[label][fn.$timerID] = window.setInterval(handler,interval); |
|---|
| 90 | |
|---|
| 91 | if ( !this.global[label] ) |
|---|
| 92 | this.global[label] = []; |
|---|
| 93 | this.global[label].push( element ); |
|---|
| 94 | |
|---|
| 95 | }, |
|---|
| 96 | remove: function(element, label, fn) { |
|---|
| 97 | var timers = element.$timers, ret; |
|---|
| 98 | |
|---|
| 99 | if ( timers ) { |
|---|
| 100 | |
|---|
| 101 | if (!label) { |
|---|
| 102 | for ( label in timers ) |
|---|
| 103 | this.remove(element, label, fn); |
|---|
| 104 | } else if ( timers[label] ) { |
|---|
| 105 | if ( fn ) { |
|---|
| 106 | if ( fn.$timerID ) { |
|---|
| 107 | window.clearInterval(timers[label][fn.$timerID]); |
|---|
| 108 | delete timers[label][fn.$timerID]; |
|---|
| 109 | } |
|---|
| 110 | } else { |
|---|
| 111 | for ( var fn in timers[label] ) { |
|---|
| 112 | window.clearInterval(timers[label][fn]); |
|---|
| 113 | delete timers[label][fn]; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | for ( ret in timers[label] ) break; |
|---|
| 118 | if ( !ret ) { |
|---|
| 119 | ret = null; |
|---|
| 120 | delete timers[label]; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | for ( ret in timers ) break; |
|---|
| 125 | if ( !ret ) |
|---|
| 126 | element.$timers = null; |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | }); |
|---|
| 131 | |
|---|
| 132 | if (jQuery.browser.msie) |
|---|
| 133 | jQuery(window).one("unload", function() { |
|---|
| 134 | var global = jQuery.timer.global; |
|---|
| 135 | for ( var label in global ) { |
|---|
| 136 | var els = global[label], i = els.length; |
|---|
| 137 | while ( --i ) |
|---|
| 138 | jQuery.timer.remove(els[i], label); |
|---|
| 139 | } |
|---|
| 140 | }); |
|---|
| 141 | |
|---|
| 142 | |
|---|