|
1 |
| -(function(root, undefined) { |
| 1 | +(function () { |
2 | 2 |
|
3 |
| - function Timer(duration, loop, wait, start, callback) { |
| 3 | + function Timer(duration, attributes, callback) { |
4 | 4 | this.timerDuration = duration;
|
5 | 5 | this.callback = callback;
|
6 |
| - this.loop = loop; |
| 6 | + this.loop = attributes.loop; |
7 | 7 | this.started = false;
|
8 | 8 | this.stopped = false; // If stop() is called this variable will be used to finish the paused duration once it's started again.
|
9 | 9 | this.timer;
|
10 | 10 | this.startTick;
|
11 | 11 | this.endTick;
|
12 | 12 |
|
13 |
| - if(start) { |
14 |
| - if(wait > 0) { |
| 13 | + if (attributes.start) { |
| 14 | + if (attributes.wait > 0) { |
15 | 15 | var self = this;
|
16 |
| - setTimeout(function() { |
| 16 | + setTimeout(function () { |
| 17 | + if (attributes.executeAfterWait) { |
| 18 | + callback(); |
| 19 | + } |
17 | 20 | self.start();
|
18 |
| - }, wait); |
| 21 | + }, attributes.wait); |
19 | 22 | } else {
|
20 | 23 | this.start();
|
21 | 24 | }
|
22 | 25 | }
|
23 | 26 | }
|
24 | 27 |
|
25 |
| - Timer.prototype.start = function() { |
26 |
| - if(!this.started) { |
| 28 | + Timer.prototype.start = function () { |
| 29 | + if (!this.started) { |
27 | 30 |
|
28 | 31 | var self = this;
|
29 | 32 |
|
30 | 33 | // Takes care of restarts. If the timer has been stopped, this will make sure the leftover duration is executed.
|
31 |
| - if(this.stopped) { |
32 |
| - setTimeout(function() { |
| 34 | + if (this.stopped) { |
| 35 | + setTimeout(function () { |
33 | 36 | self.callback();
|
34 | 37 | return self.start();
|
35 | 38 | }, this.getRemainingDuration());
|
|
38 | 41 | return true;
|
39 | 42 | }
|
40 | 43 |
|
41 |
| - if(this.loop) { |
42 |
| - this.timer = setInterval(function(){ |
| 44 | + if (this.loop) { |
| 45 | + this.timer = setInterval(function () { |
43 | 46 | self.updateStartEndTickFromDuration(self.timerDuration);
|
44 | 47 | return self.callback();
|
45 | 48 | }, this.timerDuration);
|
46 | 49 | } else {
|
47 |
| - this.timer = setTimeout(function(){ |
| 50 | + this.timer = setTimeout(function () { |
48 | 51 | self.started = false;
|
49 | 52 | return self.callback();
|
50 | 53 | }, this.timerDuration);
|
|
59 | 62 | return false;
|
60 | 63 | }
|
61 | 64 |
|
62 |
| - Timer.prototype.stop = function() { |
63 |
| - if(this.started) { |
| 65 | + Timer.prototype.stop = function () { |
| 66 | + if (this.started) { |
64 | 67 | this.clearTimer();
|
65 | 68 | this.updateStartEndTickFromDuration(this.getRemainingDuration());
|
66 | 69 | this.started = false;
|
|
71 | 74 | return false;
|
72 | 75 | }
|
73 | 76 |
|
74 |
| - Timer.prototype.clearTimer = function() { |
75 |
| - if(this.timer) { |
76 |
| - if(this.loop) { |
| 77 | + Timer.prototype.clearTimer = function () { |
| 78 | + if (this.timer) { |
| 79 | + if (this.loop) { |
77 | 80 | this.timer = clearInterval(this.timer);
|
78 | 81 | } else {
|
79 | 82 | this.timer = clearTimeout(this.timer);
|
|
85 | 88 | return false;
|
86 | 89 | }
|
87 | 90 |
|
88 |
| - Timer.prototype.updateStartEndTickFromDuration = function(duration) { |
| 91 | + Timer.prototype.updateStartEndTickFromDuration = function (duration) { |
89 | 92 | this.startTick = Date.now();
|
90 | 93 | this.endTick = this.startTick + duration;
|
91 | 94 |
|
92 | 95 | return true;
|
93 | 96 | }
|
94 | 97 |
|
95 |
| - Timer.prototype.duration = function() { |
96 |
| - if(arguments.length > 0) { |
| 98 | + Timer.prototype.duration = function () { |
| 99 | + if (arguments.length > 0) { |
97 | 100 | this.timerDuration = moment.duration(arguments[0], arguments[1]).asMilliseconds();
|
98 | 101 | this.stop();
|
99 | 102 | this.start();
|
|
103 | 106 | return false;
|
104 | 107 | }
|
105 | 108 |
|
106 |
| - Timer.prototype.getRemainingDuration = function() { |
107 |
| - if(this.startTick && this.endTick) { |
108 |
| - if(this.stopped) { |
| 109 | + Timer.prototype.getDuration = function () { |
| 110 | + return this.timerDuration; |
| 111 | + } |
| 112 | + |
| 113 | + Timer.prototype.getRemainingDuration = function () { |
| 114 | + if (this.startTick && this.endTick) { |
| 115 | + if (this.stopped) { |
109 | 116 | return this.endTick - this.startTick;
|
110 | 117 | } else {
|
111 | 118 | return this.endTick - Date.now();
|
|
115 | 122 | return 0;
|
116 | 123 | }
|
117 | 124 |
|
118 |
| - Timer.prototype.isStopped = function() { |
| 125 | + Timer.prototype.isStopped = function () { |
119 | 126 | return this.stopped;
|
120 | 127 | }
|
121 |
| - |
| 128 | + |
122 | 129 | // define internal moment reference
|
123 | 130 | var moment;
|
124 | 131 |
|
|
135 | 142 | throw "Moment Timer cannot find Moment.js";
|
136 | 143 | }
|
137 | 144 |
|
138 |
| - moment.duration.fn.timer = function(attributes, callback) { |
139 |
| - if(typeof attributes === "function") { |
| 145 | + moment.duration.fn.timer = function (attributes, callback) { |
| 146 | + if (typeof attributes === "function") { |
140 | 147 | callback = attributes;
|
141 | 148 | attributes = {
|
142 | 149 | wait: 0,
|
143 | 150 | loop: false,
|
144 | 151 | start: true
|
145 | 152 | };
|
146 |
| - } else if(typeof attributes === "object" && typeof callback === "function") { |
147 |
| - if(attributes.start == null) { |
| 153 | + } else if (typeof attributes === "object" && typeof callback === "function") { |
| 154 | + if (attributes.start == null) { |
148 | 155 | attributes.start = true;
|
149 | 156 | }
|
150 | 157 | } else {
|
151 | 158 | throw new Error("First argument must be of type function or object.");
|
152 | 159 | }
|
153 | 160 |
|
154 |
| - return new Timer(this.asMilliseconds(), attributes.loop, attributes.wait, attributes.start, callback); |
| 161 | + return new Timer(this.asMilliseconds(), attributes, callback); |
155 | 162 | };
|
156 | 163 |
|
157 | 164 | })(this);
|
0 commit comments