-
Notifications
You must be signed in to change notification settings - Fork 0
/
tock.js
231 lines (199 loc) · 4.6 KB
/
tock.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Tock by Mr Chimp - github.com/mrchimp/tock
* Based on code by James Edwards:
* sitepoint.com/creating-accurate-timers-in-javascript/
*/
var Tock = (function(options) {
Tock.instances = (Tock.instances || 0) + 1;
var go = false,
interval = options.interval || 10,
countdown = options.countdown || false,
start_time = 0,
pause_time = 0,
final_time = 0,
duration_ms = 0,
time = 0,
elapsed = 0,
callback = options.callback,
complete = options.complete,
prefix = "Tock" + Tock.instances;
/**
* Reset the clock
*/
function reset() {
if (countdown) {
return false;
}
stop();
start_time = 0;
time = 0;
elapsed = '0.0';
}
/**
* Start the clock.
*/
function start(time) {
start_time = time;
if (countdown) {
_startCountdown(time);
} else {
_startTimer(0);
}
}
/**
* Called every tick for countdown clocks.
* i.e. once every this.interval ms
*/
function _tick() {
time += interval;
elapsed = Math.floor(time / interval) / 10;
if (Math.round(elapsed) === elapsed) { elapsed += '.0'; }
var t = this,
diff = (Date.now() - start_time) - time,
next_interval_in = interval - diff;
if (callback !== undefined) {
callback(this);
}
if (countdown && (duration_ms - time < 0)) {
final_time = 0;
go = false;
complete();
}
if (next_interval_in <= 0) {
missed_ticks = Math.floor(Math.abs(next_interval_in) / interval);
time += missed_ticks * interval;
if (go) {
_tick();
}
} else {
if (go) {
timeout = window.setTimeout(_tick, next_interval_in);
timeout = prefix + timeout;
}
}
}
/**
* Stop the clock.
*/
function stop() {
go = false;
window.clearTimeout(timeout.replace(prefix));
if (countdown) {
final_time = duration_ms - time;
} else {
final_time = (Date.now() - start_time);
}
}
/**
* Stop/start the clock.
*/
function pause() {
if (go) {
pause_time = lap();
stop();
} else {
if (pause_time) {
if (countdown) {
_startCountdown(pause_time);
} else {
_startTimer(pause_time);
}
}
}
}
/**
* Get the current clock time in ms.
* Use with Tock.msToTime() to make it look nice.
*/
function lap() {
if (go) {
var now;
if (countdown) {
now = duration_ms - (Date.now() - start_time);
} else {
now = (Date.now() - start_time);
}
return now;
}
return pause_time || final_time;
}
/**
* Format milliseconds as a string.
*/
function msToTime(ms) {
if (ms <= 0) {
return "00:00.000";
}
var milliseconds = (ms % 1000).toString(),
seconds = Math.floor((ms / 1000) % 60).toString(),
minutes = Math.floor((ms / (60 * 1000)) % 60).toString();
if (milliseconds.length === 1) {
milliseconds = '00' + milliseconds;
} else if (milliseconds.length === 2) {
milliseconds = '0' + milliseconds;
}
if (seconds.length === 1) {
seconds = '0' + seconds;
}
if (minutes.length === 1) {
minutes = '0' + minutes;
}
return minutes + ":" + seconds + "." + milliseconds;
}
/**
* Convert a time string to milliseconds
* Todo: handle this a bit better
*
* Possible inputs:
* MM:SS
* MM:SS:ms
* yyyy-mm-dd HH:MM:SS.ms
*/
function timeToMS(time) {
var ms = new Date(time).getTime();
if (!ms) {
var time_split = time.split(':');
ms = parseInt(time_split[0], 10) * 60000;
if (time_split.length > 1) {
ms += parseInt(time_split[1], 10) * 1000;
}
if (time_split.length > 2) {
ms += parseInt(time_split[2], 10);
}
}
return ms;
}
/**
* Called by Tock internally - use start() instead
*/
function _startCountdown(duration) {
duration_ms = duration;
start_time = Date.now();
end_time = start_time + duration_ms;
time = 0;
elapsed = '0.0';
go = true;
_tick();
this.timeout = window.setTimeout(_tick, interval);
}
/**
* Called by Tock internally - use start() instead
*/
function _startTimer(start_offset) {
start_time = Date.now() - start_offset;
time = 0;
elapsed = '0.0';
go = true;
_tick();
this.timeout = window.setTimeout(_tick, interval);
}
return {
start: start,
pause: pause,
stop: stop,
reset: reset,
lap: lap,
msToTime: msToTime,
timeToMS: timeToMS
};
});