-
Notifications
You must be signed in to change notification settings - Fork 6
/
stopwatch.js
135 lines (130 loc) · 3.94 KB
/
stopwatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Javascript Stopwatch class
* http://www.seph.dk
*
* Copyright 2009 Seph soliman
* Released under the CC BY 4.0 (do whatever you want - just leave my name on it)
* https://creativecommons.org/licenses/by/4.0/
*/
// * Stopwatch class
Stopwatch = function(listener, resolution, countUp) {
this.startTime = 0;
this.stopTime = 0;
this.totalElapsed = 0; // * elapsed number of ms in total
this.started = false;
this.listener = (listener != undefined ? listener : null); // * function to receive onTick events
this.countUp = typeof countUp !== 'undefined' ? countUp : true;
this.tickResolution = (resolution != undefined ? resolution : 500); // * how long between each tick in milliseconds
this.tickInterval = null;
// * pretty static vars
this.onehour = 1000 * 60 * 60;
this.onemin = 1000 * 60;
this.onesec = 1000;
}
Stopwatch.prototype.start = function() {
var delegate = function(that, method) { return function() { return method.call(that) } };
if(!this.started) {
this.startTime = new Date().getTime();
this.stopTime = 0;
this.started = true;
this.tickInterval = setInterval(delegate(this, this.onTick), this.tickResolution);
}
}
Stopwatch.prototype.stop = function() {
if(this.started) {
this.stopTime = new Date().getTime();
this.started = false;
var elapsed = this.stopTime - this.startTime;
this.totalElapsed += elapsed;
if(this.tickInterval != null)
clearInterval(this.tickInterval);
}
return this.getElapsed();
}
Stopwatch.prototype.reset = function() {
this.totalElapsed = 0;
// * if watch is running, reset it to current time
this.startTime = new Date().getTime();
this.stopTime = this.startTime;
if (!this.countUp) {
this.totalElapsed = this.initialElapsed;
}
if (this.tickInterval != null) {
var delegate = function(that, method) {
return function() {
return method.call(that);
};
};
clearInterval(this.tickInterval);
this.tickInterval = setInterval(delegate(this, this.onTick),
this.tickResolution);
}
}
Stopwatch.prototype.restart = function() {
this.stop();
this.reset();
this.start();
}
Stopwatch.prototype.getElapsed = function() {
// * if watch is stopped, use that date, else use now
var elapsed = 0;
if(this.started)
elapsed = new Date().getTime() - this.startTime;
elapsed += this.totalElapsed;
if (!this.countUp) {
elapsed = Math.max(2*this.initialElapsed - elapsed, 0);
}
var hours = parseInt(elapsed / this.onehour);
elapsed %= this.onehour;
var mins = parseInt(elapsed / this.onemin);
elapsed %= this.onemin;
var secs = parseInt(elapsed / this.onesec);
var ms = elapsed % this.onesec;
return {
hours: hours,
minutes: mins,
seconds: secs,
milliseconds: ms
};
}
Stopwatch.prototype.setElapsed = function(hours, mins, secs) {
// this.reset();
this.totalElapsed = 0;
this.startTime = new Date().getTime();
this.stopTime = this.startTime;
this.totalElapsed += hours * this.onehour;
this.totalElapsed += mins * this.onemin;
this.totalElapsed += this.countUp ? secs * this.onesec : (secs+1)*this.onesec-1;
this.totalElapsed = Math.max(this.totalElapsed, 0); // * No negative numbers
this.initialElapsed = this.totalElapsed;
if (this.tickInterval != null) {
var delegate = function(that, method) {
return function() {
return method.call(that);
};
};
clearInterval(this.tickInterval);
this.tickInterval = setInterval(delegate(this, this.onTick),
this.tickResolution);
}
}
Stopwatch.prototype.toString = function(milliseconds) {
var milliseconds = milliseconds || false;
var zpad = function(no, digits) {
no = no.toString();
while(no.length < digits)
no = '0' + no;
return no;
}
var e = this.getElapsed();
return zpad(e.hours,2) + ":" + zpad(e.minutes,2) + ":" + zpad(e.seconds,2) + (milliseconds ? '.' + zpad(e.milliseconds, 3) : '');
}
Stopwatch.prototype.setListener = function(listener) {
this.listener = listener;
}
// * triggered every <resolution> ms
Stopwatch.prototype.onTick = function() {
if(this.listener != null) {
this.listener(this);
}
}