|
27 | 27 | */
|
28 | 28 |
|
29 | 29 | ;(function($) {
|
30 |
| - $.timer = function(func, time, autostart) { |
31 |
| - this.set = function(func, time, autostart) { |
32 |
| - this.init = true; |
33 |
| - if(typeof func == 'object') { |
34 |
| - var paramList = ['autostart', 'time']; |
35 |
| - for(var arg in paramList) {if(func[paramList[arg]] != undefined) {eval(paramList[arg] + " = func[paramList[arg]]");}}; |
36 |
| - func = func.action; |
37 |
| - } |
38 |
| - if(typeof func == 'function') {this.action = func;} |
39 |
| - if(!isNaN(time)) {this.intervalTime = time;} |
40 |
| - if(autostart && !this.isActive) { |
41 |
| - this.isActive = true; |
42 |
| - this.setTimer(); |
43 |
| - } |
44 |
| - return this; |
45 |
| - }; |
46 |
| - this.once = function(time) { |
47 |
| - var timer = this; |
48 |
| - if(isNaN(time)) {time = 0;} |
49 |
| - setTimeout(function() {timer.action();}, time); |
50 |
| - return this; |
51 |
| - }; |
52 |
| - this.play = function(reset) { |
53 |
| - if(!this.isActive) { |
54 |
| - if(reset) {this.setTimer();} |
55 |
| - else {this.setTimer(this.remaining);} |
56 |
| - this.isActive = true; |
57 |
| - } |
58 |
| - return this; |
59 |
| - }; |
60 |
| - this.pause = function() { |
61 |
| - if(this.isActive) { |
62 |
| - this.isActive = false; |
63 |
| - this.remaining -= new Date() - this.last; |
64 |
| - this.clearTimer(); |
65 |
| - } |
66 |
| - return this; |
67 |
| - }; |
68 |
| - this.stop = function() { |
69 |
| - this.isActive = false; |
70 |
| - this.remaining = this.intervalTime; |
71 |
| - this.clearTimer(); |
72 |
| - return this; |
73 |
| - }; |
74 |
| - this.toggle = function(reset) { |
75 |
| - if(this.isActive) {this.pause();} |
76 |
| - else if(reset) {this.play(true);} |
77 |
| - else {this.play();} |
78 |
| - return this; |
79 |
| - }; |
80 |
| - this.reset = function() { |
81 |
| - this.isActive = false; |
82 |
| - this.play(true); |
83 |
| - return this; |
84 |
| - }; |
85 |
| - this.clearTimer = function() { |
86 |
| - clearTimeout(this.timeoutObject); |
87 |
| - }; |
88 |
| - this.setTimer = function(time) { |
89 |
| - var timer = this; |
90 |
| - if(typeof this.action != 'function') {return;} |
91 |
| - if(isNaN(time)) {time = this.intervalTime;} |
92 |
| - this.remaining = time; |
93 |
| - this.last = new Date(); |
94 |
| - this.clearTimer(); |
95 |
| - this.timeoutObject = setTimeout(function() {timer.go();}, time); |
96 |
| - }; |
97 |
| - this.go = function() { |
98 |
| - if(this.isActive) { |
99 |
| - try { this.action(); } |
100 |
| - finally { this.setTimer(); } |
101 |
| - } |
102 |
| - }; |
103 |
| - |
104 |
| - if(this.init) { |
105 |
| - return new $.timer(func, time, autostart); |
106 |
| - } else { |
107 |
| - this.set(func, time, autostart); |
108 |
| - return this; |
109 |
| - } |
110 |
| - }; |
| 30 | + |
| 31 | + $.timer = Timer; |
| 32 | + |
| 33 | + /** |
| 34 | + * First parameter can either be a function or an object of parameters. |
| 35 | + * |
| 36 | + * @param {function | { |
| 37 | + * action: function, |
| 38 | + * time: int=, |
| 39 | + * autostart: boolean= |
| 40 | + * }} action |
| 41 | + * @param {int=} time |
| 42 | + * @param {boolean=} autostart |
| 43 | + * @returns {Timer} |
| 44 | + */ |
| 45 | + function Timer(action, time, autostart) { |
| 46 | + |
| 47 | + if (this.constructor != Timer || this.init) { |
| 48 | + return new Timer(action, time, autostart); |
| 49 | + } |
| 50 | + |
| 51 | + this.set(action, time, autostart); |
| 52 | + |
| 53 | + return this; |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @see Timer |
| 59 | + * |
| 60 | + * @param {function | { |
| 61 | + * action: function, |
| 62 | + * time: int=, |
| 63 | + * autostart: boolean= |
| 64 | + * }} action |
| 65 | + * @param {int=} time |
| 66 | + * @param {boolean=} autostart |
| 67 | + * @returns {Timer} |
| 68 | + */ |
| 69 | + Timer.prototype.set = function(action, time, autostart) { |
| 70 | + |
| 71 | + this.init = true; |
| 72 | + |
| 73 | + if (typeof action == "object") { |
| 74 | + |
| 75 | + if (action.time) { |
| 76 | + time = action.time; |
| 77 | + } |
| 78 | + |
| 79 | + if (action.autostart) { |
| 80 | + autostart = action.autostart; |
| 81 | + } |
| 82 | + |
| 83 | + action = action.action; |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + if (typeof action == "function") { |
| 88 | + this.action = action; |
| 89 | + } |
| 90 | + |
| 91 | + if (!isNaN(time)) { |
| 92 | + this.intervalTime = time; |
| 93 | + } |
| 94 | + |
| 95 | + if (autostart && this.isReadyToStart()) { |
| 96 | + this.isActive = true; |
| 97 | + this.setTimer(); |
| 98 | + } |
| 99 | + |
| 100 | + return this; |
| 101 | + |
| 102 | + }; |
| 103 | + |
| 104 | + Timer.prototype.isReadyToStart = function() { |
| 105 | + |
| 106 | + var notActive = !this.active; |
| 107 | + var hasAction = typeof this.action == "function"; |
| 108 | + var hasTime = !isNaN(this.intervalTime); |
| 109 | + |
| 110 | + return notActive && hasAction && hasTime; |
| 111 | + |
| 112 | + }; |
| 113 | + |
| 114 | + /** |
| 115 | + * @param {int=} time |
| 116 | + * @returns {Timer} |
| 117 | + */ |
| 118 | + Timer.prototype.once = function(time) { |
| 119 | + |
| 120 | + var timer = this; |
| 121 | + |
| 122 | + if (isNaN(time)) { |
| 123 | + timer.action(); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + setTimeout(fnTimeout, time); |
| 128 | + return this; |
| 129 | + |
| 130 | + function fnTimeout() { |
| 131 | + timer.action(); |
| 132 | + } |
| 133 | + |
| 134 | + }; |
| 135 | + |
| 136 | + /** |
| 137 | + * @param {boolean=} reset |
| 138 | + * @returns {Timer} |
| 139 | + */ |
| 140 | + Timer.prototype.play = function(reset) { |
| 141 | + |
| 142 | + if (this.isReadyToStart()) { |
| 143 | + |
| 144 | + if (reset) { |
| 145 | + this.setTimer(); |
| 146 | + } |
| 147 | + else { |
| 148 | + this.setTimer(this.remaining); |
| 149 | + } |
| 150 | + |
| 151 | + this.isActive = true; |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + return this; |
| 156 | + |
| 157 | + }; |
| 158 | + |
| 159 | + /** |
| 160 | + * @returns {Timer} |
| 161 | + */ |
| 162 | + Timer.prototype.pause = function() { |
| 163 | + |
| 164 | + if (this.isActive) { |
| 165 | + |
| 166 | + this.isActive = false; |
| 167 | + this.remaining -= new Date() - this.last; |
| 168 | + |
| 169 | + this.clearTimer(); |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + return this; |
| 174 | + |
| 175 | + }; |
| 176 | + |
| 177 | + /** |
| 178 | + * @returns {Timer} |
| 179 | + */ |
| 180 | + Timer.prototype.stop = function() { |
| 181 | + |
| 182 | + this.isActive = false; |
| 183 | + this.remaining = this.intervalTime; |
| 184 | + |
| 185 | + this.clearTimer(); |
| 186 | + |
| 187 | + return this; |
| 188 | + |
| 189 | + }; |
| 190 | + |
| 191 | + /** |
| 192 | + * @param {boolean=} reset |
| 193 | + * @returns {Timer} |
| 194 | + */ |
| 195 | + Timer.prototype.toggle = function(reset) { |
| 196 | + |
| 197 | + if (this.isActive) { |
| 198 | + this.pause(); |
| 199 | + } |
| 200 | + else if (reset) { |
| 201 | + this.play(true); |
| 202 | + } |
| 203 | + else { |
| 204 | + this.play(); |
| 205 | + } |
| 206 | + |
| 207 | + return this; |
| 208 | + |
| 209 | + }; |
| 210 | + |
| 211 | + /** |
| 212 | + * @returns {Timer} |
| 213 | + */ |
| 214 | + Timer.prototype.reset = function() { |
| 215 | + |
| 216 | + this.isActive = false; |
| 217 | + this.play(true); |
| 218 | + |
| 219 | + return this; |
| 220 | + |
| 221 | + }; |
| 222 | + |
| 223 | + /** |
| 224 | + * @returns {Timer} |
| 225 | + */ |
| 226 | + Timer.prototype.clearTimer = function() { |
| 227 | + clearTimeout(this.timeoutObject); |
| 228 | + return this; |
| 229 | + }; |
| 230 | + |
| 231 | + /** |
| 232 | + * @returns {Timer} |
| 233 | + */ |
| 234 | + Timer.prototype.setTimer = function(time) { |
| 235 | + |
| 236 | + var timer = this; |
| 237 | + |
| 238 | + if (isNaN(time)) { |
| 239 | + time = this.intervalTime; |
| 240 | + } |
| 241 | + |
| 242 | + this.remaining = time; |
| 243 | + this.last = new Date(); |
| 244 | + |
| 245 | + this.clearTimer(); |
| 246 | + |
| 247 | + this.timeoutObject = setTimeout(fnTimeout, time); |
| 248 | + |
| 249 | + return this; |
| 250 | + |
| 251 | + function fnTimeout() { |
| 252 | + timer.execute(); |
| 253 | + } |
| 254 | + |
| 255 | + }; |
| 256 | + |
| 257 | + /** |
| 258 | + * @returns {Timer} |
| 259 | + */ |
| 260 | + Timer.prototype.execute = function() { |
| 261 | + |
| 262 | + if (this.isActive) { |
| 263 | + |
| 264 | + try { |
| 265 | + this.action(); |
| 266 | + } |
| 267 | + finally { |
| 268 | + this.setTimer(); |
| 269 | + } |
| 270 | + |
| 271 | + } |
| 272 | + |
| 273 | + return this; |
| 274 | + |
| 275 | + }; |
| 276 | + |
111 | 277 | })(jQuery);
|
0 commit comments