Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit e89dd1b

Browse files
committed
added getDuration and executeAfterWait attribute
1 parent f59b5fa commit e89dd1b

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moment-timer",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "A moment.js plugin for timers setInterval & setTimeout.",
55
"main": "lib/moment-timer.js",
66
"repository": {
@@ -25,4 +25,4 @@
2525
"bugs": {
2626
"url": "https://github.com/SeverinDK/moment-timer/issues"
2727
}
28-
}
28+
}

example/index.html

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,15 @@ <h1>Example of Interval.</h1>
5555
var intervalContainer = document.getElementById("interval");
5656
var startTick = new Date().getTime();
5757
var interval = moment.duration(1, "seconds").timer({
58-
loop: true
58+
loop: true,
59+
wait: 2500,
60+
executeAfterWait: true
5961
},
6062
function() {
6163
intervalContainer.innerHTML += "Callback fired "
6264
+ (new Date().getTime() - startTick)
6365
+ "ms after script was started.<br>";
6466
});
65-
66-
moment.duration(1500).timer({loop: false}, function() {
67-
interval.stop();
68-
moment.duration(1500).timer(function() {
69-
interval.start();
70-
});
71-
});
7267
</script>
7368
</div>
7469
</body>

lib/moment-timer.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
(function(root, undefined) {
1+
(function () {
22

3-
function Timer(duration, loop, wait, start, callback) {
3+
function Timer(duration, attributes, callback) {
44
this.timerDuration = duration;
55
this.callback = callback;
6-
this.loop = loop;
6+
this.loop = attributes.loop;
77
this.started = false;
88
this.stopped = false; // If stop() is called this variable will be used to finish the paused duration once it's started again.
99
this.timer;
1010
this.startTick;
1111
this.endTick;
1212

13-
if(start) {
14-
if(wait > 0) {
13+
if (attributes.start) {
14+
if (attributes.wait > 0) {
1515
var self = this;
16-
setTimeout(function() {
16+
setTimeout(function () {
17+
if (attributes.executeAfterWait) {
18+
callback();
19+
}
1720
self.start();
18-
}, wait);
21+
}, attributes.wait);
1922
} else {
2023
this.start();
2124
}
2225
}
2326
}
2427

25-
Timer.prototype.start = function() {
26-
if(!this.started) {
28+
Timer.prototype.start = function () {
29+
if (!this.started) {
2730

2831
var self = this;
2932

3033
// 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 () {
3336
self.callback();
3437
return self.start();
3538
}, this.getRemainingDuration());
@@ -38,13 +41,13 @@
3841
return true;
3942
}
4043

41-
if(this.loop) {
42-
this.timer = setInterval(function(){
44+
if (this.loop) {
45+
this.timer = setInterval(function () {
4346
self.updateStartEndTickFromDuration(self.timerDuration);
4447
return self.callback();
4548
}, this.timerDuration);
4649
} else {
47-
this.timer = setTimeout(function(){
50+
this.timer = setTimeout(function () {
4851
self.started = false;
4952
return self.callback();
5053
}, this.timerDuration);
@@ -59,8 +62,8 @@
5962
return false;
6063
}
6164

62-
Timer.prototype.stop = function() {
63-
if(this.started) {
65+
Timer.prototype.stop = function () {
66+
if (this.started) {
6467
this.clearTimer();
6568
this.updateStartEndTickFromDuration(this.getRemainingDuration());
6669
this.started = false;
@@ -71,9 +74,9 @@
7174
return false;
7275
}
7376

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) {
7780
this.timer = clearInterval(this.timer);
7881
} else {
7982
this.timer = clearTimeout(this.timer);
@@ -85,15 +88,15 @@
8588
return false;
8689
}
8790

88-
Timer.prototype.updateStartEndTickFromDuration = function(duration) {
91+
Timer.prototype.updateStartEndTickFromDuration = function (duration) {
8992
this.startTick = Date.now();
9093
this.endTick = this.startTick + duration;
9194

9295
return true;
9396
}
9497

95-
Timer.prototype.duration = function() {
96-
if(arguments.length > 0) {
98+
Timer.prototype.duration = function () {
99+
if (arguments.length > 0) {
97100
this.timerDuration = moment.duration(arguments[0], arguments[1]).asMilliseconds();
98101
this.stop();
99102
this.start();
@@ -103,9 +106,13 @@
103106
return false;
104107
}
105108

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) {
109116
return this.endTick - this.startTick;
110117
} else {
111118
return this.endTick - Date.now();
@@ -115,10 +122,10 @@
115122
return 0;
116123
}
117124

118-
Timer.prototype.isStopped = function() {
125+
Timer.prototype.isStopped = function () {
119126
return this.stopped;
120127
}
121-
128+
122129
// define internal moment reference
123130
var moment;
124131

@@ -135,23 +142,23 @@
135142
throw "Moment Timer cannot find Moment.js";
136143
}
137144

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") {
140147
callback = attributes;
141148
attributes = {
142149
wait: 0,
143150
loop: false,
144151
start: true
145152
};
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) {
148155
attributes.start = true;
149156
}
150157
} else {
151158
throw new Error("First argument must be of type function or object.");
152159
}
153160

154-
return new Timer(this.asMilliseconds(), attributes.loop, attributes.wait, attributes.start, callback);
161+
return new Timer(this.asMilliseconds(), attributes, callback);
155162
};
156163

157164
})(this);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moment-timer",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "A moment.js plugin for timers setInterval & setTimeout.",
55
"main": "lib/moment-timer.js",
66
"repository": {

0 commit comments

Comments
 (0)