-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtock.js
335 lines (287 loc) · 9.32 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Tock by Mr Chimp - github.com/mrchimp/tock
* Based on code by James Edwards:
* sitepoint.com/creating-accurate-timers-in-javascript/
*/
// Implements Date.now() for ie lt 9
Date.now = Date.now || function() { return +new Date(); };
// Polyfills Function.prototype.bind for IE lt 9 and Safari lt 5.1
if ( typeof Function.prototype.bind != 'function' ) {
Function.prototype.bind = function (ctx) {
var args = Array.prototype.slice.call(arguments, 1),
fn = this;
return function () {
args.push.apply(args, arguments);
return fn.apply(ctx, args);
};
};
}
(function (root, factory) {
if ( typeof define === 'function' && define.amd ) {
define(factory);
} else if ( typeof exports === 'object' ) {
module.exports = factory();
} else {
root.Tock = factory();
}
}(this, function () {
/**
* Called every tick for countdown clocks.
* i.e. once every this.interval ms
*/
function _tick () {
this.time += this.interval;
if ( this.countdown && (this.duration_ms - this.time < 0) ) {
this.final_time = 0;
this.go = false;
this.callback(this);
clearTimeout(this.timeout);
this.complete(this);
return;
} else {
this.callback(this);
}
var diff = (Date.now() - this.start_time) - this.time,
next_interval_in = diff > 0 ? this.interval - diff : this.interval;
if ( next_interval_in <= 0 ) {
this.missed_ticks = Math.floor(Math.abs(next_interval_in) / this.interval);
this.time += this.missed_ticks * this.interval;
if ( this.go ) {
_tick.call(this);
}
} else if ( this.go ) {
this.timeout = setTimeout(_tick.bind(this), next_interval_in);
}
}
/**
* Called by Tock internally - use start() instead
*/
function _startCountdown (duration) {
this.duration_ms = duration;
this.start_time = Date.now();
this.time = 0;
this.go = true;
_tick.call(this);
}
/**
* Called by Tock internally - use start() instead
*/
function _startTimer (start_offset) {
this.start_time = start_offset || Date.now();
this.time = 0;
this.go = true;
_tick.call(this);
}
var MILLISECONDS_RE = /^\s*(\+|-)?\d+\s*$/,
MM_SS_RE = /^(\d{1,2}):(\d{2})$/,
MM_SS_ms_OR_HH_MM_SS_RE = /^(\d{1,2}):(\d{2})(?::|\.)(\d{2,3})$/,
MS_PER_HOUR = 3600000,
MS_PER_MIN = 60000,
MS_PER_SEC = 1000,
/* The RegExp below will match a date in format `yyyy-mm-dd HH:MM:SS` and optionally with `.ms` at the end.
* It will also match ISO date string, i.e. if the whitespace separator in the middle is replaced with a `T`
* and the date string is also suffixed with a `Z` denoting UTC timezone.
*/
yyyy_mm_dd_HH_MM_SS_ms_RE = /^(\d{4})-([0-1]\d)-([0-3]\d)(?:\s|T)(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3})Z?)?$/;
var Tock = function (options) {
options = options || {};
if ( ! (this instanceof Tock) ) return new Tock(options);
Tock.instances = (Tock.instances || 0) + 1;
this.go = false;
this.timeout = null;
this.missed_ticks = null;
this.interval = options.interval || 10;
this.countdown = options.countdown || false;
this.start_time = 0;
this.pause_time = 0;
this.final_time = 0;
this.duration_ms = 0;
this.time = 0;
this.callback = options.callback || function () {};
this.complete = options.complete || function () {};
};
/**
* Reset the clock
*/
Tock.prototype.reset = function () {
if ( this.countdown ) {
return false;
}
this.stop();
this.start_time = 0;
this.time = 0;
};
/**
* Start the clock.
* @param {Various} time Accepts a single "time" argument
* which can be in various forms:
* - MM:SS
* - MM:SS:ms or MM:SS.ms
* - HH:MM:SS
* - yyyy-mm-dd HH:MM:SS.ms
* - milliseconds
*/
Tock.prototype.start = function (time) {
if (this.go) return false;
time = time ? this.timeToMS(time) : 0;
this.start_time = time;
this.pause_time = 0;
if ( this.countdown ) {
_startCountdown.call(this, time);
} else {
_startTimer.call(this, Date.now() - time);
}
};
/**
* Stop the clock and clear the timeout
*/
Tock.prototype.stop = function () {
this.pause_time = this.lap();
this.go = false;
clearTimeout(this.timeout);
if ( this.countdown ) {
this.final_time = this.duration_ms - this.time;
} else {
this.final_time = (Date.now() - this.start_time);
}
};
/**
* Stop/start the clock.
*/
Tock.prototype.pause = function () {
if ( this.go ) {
this.pause_time = this.lap();
this.stop();
} else {
if ( this.pause_time ) {
if ( this.countdown ) {
_startCountdown.call(this, this.pause_time);
} else {
_startTimer.call(this, Date.now() - this.pause_time);
}
this.pause_time = 0;
}
}
};
/**
* Get the current clock time in ms.
* Use with Tock.msToTime() to make it look nice.
* @return {Integer} Number of milliseconds ellapsed/remaining
*/
Tock.prototype.lap = function () {
if ( this.go ) {
var now;
if ( this.countdown ) {
now = this.duration_ms - (Date.now() - this.start_time);
} else {
now = (Date.now() - this.start_time);
}
return now;
}
return this.pause_time || this.final_time;
};
/**
* Format milliseconds as a MM:SS.ms string.
* @param {Integer} ms Number of milliseconds
* @return {String} String representation of ms in format MM:SS:ms
*/
Tock.prototype.msToTime = function (ms) {
var milliseconds = this.zeroPad(ms % MS_PER_SEC, 3),
seconds = this.zeroPad(Math.floor((ms / MS_PER_SEC) % 60), 2),
minutes = this.zeroPad(Math.floor((ms / (MS_PER_MIN)) % 60), 2);
return minutes + ':' + seconds + '.' + milliseconds;
};
/**
* Pad the left side of a string with zeros up to a given length. I
* considered using an NPM package for this but it's probably best not to.
* @param {Various} input Number to pad. Will be converted to string.
* @param {Integer} length Desired string length
* @return {String} Padding number
*/
Tock.prototype.zeroPad = function (input, length) {
input = input.toString();
while ( input.length < length ) {
input = '0' + input;
}
return input;
};
/**
* Format milliseconds as HH:MM:SS or HH:MM:SS:mmm
* @param {Integer} ms Number of milliseconds
* @param {Boolean} show_ms If true, include milliseconds in output
* @return {String} Formatted timecode string
*/
Tock.prototype.msToTimecode = function (ms, show_ms) {
var seconds = this.zeroPad(Math.floor((ms / MS_PER_SEC) % 60), 2),
minutes = this.zeroPad(Math.floor((ms / MS_PER_MIN) % 60), 2),
hours = this.zeroPad(Math.floor((ms / MS_PER_HOUR)), 2),
millisec = (show_ms ? ':' + this.zeroPad(Math.floor(ms % MS_PER_SEC), 3) : '');
return hours + ':' + minutes + ':' + seconds + millisec;
};
/**
* Convert a time string to milliseconds
*
* Possible inputs:
* MM:SS
* MM:SS:ms or MM:SS.ms
* HH:MM:SS
* yyyy-mm-dd HH:MM:SS.ms
*
* A milliseconds input will return it back for safety
* If the input cannot be recognized then 0 is returned
*/
Tock.prototype.timeToMS = function (time) {
// If input is milliseconds integer then return it back
if ( MILLISECONDS_RE.test(String(time)) ) {
return time;
}
var ms,
time_split,
match,
date,
now = new Date();
if ( MM_SS_RE.test(time) ) { // If MM:SS
time_split = time.split(':');
ms = parseInt(time_split[0], 10) * MS_PER_MIN;
ms += parseInt(time_split[1], 10) * MS_PER_SEC;
} else {
match = time.match(MM_SS_ms_OR_HH_MM_SS_RE);
if ( match ) {
if ( match[3].length == 3 || parseInt(match[3], 10) > 59 ) { // If MM:SS:ms or MM:SS.ms (e.g. 10:10:458 or 10:10.458)
ms = parseInt(match[1], 10) * MS_PER_MIN;
ms += parseInt(match[2], 10) * MS_PER_SEC;
ms += parseInt(match[3], 10);
} else { // Then it's HH:MM:SS
ms = parseInt(match[1], 10) * MS_PER_HOUR;
ms += parseInt(match[2], 10) * MS_PER_MIN;
ms += parseInt(match[3], 10) * MS_PER_SEC;
}
} else if ( yyyy_mm_dd_HH_MM_SS_ms_RE.test(time) ) { // If yyyy-mm-dd HH:MM:SS or yyyy-mm-dd HH:MM:SS.ms or yyyy-mm-ddTHH:MM:SS.msZ
date = new Date();
now = new Date();
match = time.match(yyyy_mm_dd_HH_MM_SS_ms_RE);
date.setYear(match[1]);
date.setMonth(match[2]);
date.setDate(match[3]);
date.setHours(match[4]);
date.setMinutes(match[5]);
date.setSeconds(match[6]);
if (typeof match[7] !== 'undefined') {
date.setMilliseconds(match[7]);
}
ms = Math.max(0, date.getTime() - now.getTime());
} else {
// Let's try it as a date string
now = new Date();
ms = Date.parse(time);
if ( !isNaN(ms) ) { // Looks ok
ms = Math.max(0, ms - now.getTime());
} else { // Could not recognize input, so start from 0
ms = 0;
}
}
}
return ms;
};
return Tock;
}));