blob: 0e2e5be35aa3c908fc9dcdbcbcd549c1a9d94b3f [file] [log] [blame]
Alexander Afanasyev4c17b482013-03-02 01:32:35 -08001/**
2 * jquery.timer.js
3 *
4 * Copyright (c) 2011 Jason Chavannes <jason.chavannes@gmail.com>
5 *
6 * http://jchavannes.com/jquery-timer
7 *
8 * Permission is hereby granted, free of charge, to any person
9 * obtaining a copy of this software and associated documentation
10 * files (the "Software"), to deal in the Software without
11 * restriction, including without limitation the rights to use, copy,
12 * modify, merge, publish, distribute, sublicense, and/or sell copies
13 * of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
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 window.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 window.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 = window.setTimeout(function() {timer.go();}, time);
96 };
97 this.go = function() {
98 if(this.isActive) {
99 this.action();
100 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 };
111})(jQuery);