-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (62 loc) · 1.72 KB
/
index.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
/*global mopdule exports global*/
;(function(exports){
"use strict";
function Timelapser(time){
this.callback = null;
this.time = time;
this.paused = undefined;
this._timer = null;
this._start = 0;
this._stop = 0;
}
Timelapser.prototype = {
_tick: function(time){
if(!this.paused){
time = time || this.time;
this._start = Date.now();
this._timer = setTimeout(this._tock.bind(this),time);
}
},
_tock: function(){
if(!this.callback || this.paused) return;
try {
this.callback();
} catch(e){
this.stop();
if(typeof(this.onerror) !== 'function') throw(e);
else this.onerror(e);
}
this._tick();
},
start: function(fn,time){
if(time) this.time = time;
else time = this.time;
if(typeof(fn) === 'function'){
this.callback = fn;
}
if(this.paused === false){
this.stop();
}
if(this._stop){
time = time - (this._stop - this._start);
this._stop = 0;
if(time <= 0) time = this.time;
}
this.paused = false;
this._tick(time);
},
stop: function(){
if(!this.paused){
this._stop = Date.now();
clearInterval(this._timer);
this.paused = true;
}
},
reset: function(){
this.paused = undefined;
this._start = 0;
this._stop = 0;
}
};
exports.Timelapser = Timelapser;
}('undefined' !== typeof exports && exports || 'undefined' !== typeof window && window || global || this));