forked from josex2r/jQuery-SlotMachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
54 lines (45 loc) · 909 Bytes
/
timer.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
module.exports = class Timer {
constructor (cb, delay) {
this.cb = cb;
this.initialDelay = delay;
this.delay = delay;
this.startTime = null;
this.timer = null;
this.running = false;
this.resume();
return this;
}
_start () {
this.timer = setTimeout(() => {
this.running = false;
this.cb(this);
}, this.delay);
}
cancel () {
this.running = false;
clearTimeout(this.timer);
}
pause () {
if (this.running) {
this.delay -= new Date().getTime() - this.startTime;
this.cancel();
}
}
resume () {
if (!this.running) {
this.running = true;
this.startTime = new Date().getTime();
this._start();
}
}
reset () {
this.cancel();
this.delay = this.initialDelay;
this._start();
}
add (extraDelay) {
this.pause();
this.delay += extraDelay;
this.resume();
}
};