-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker.js
38 lines (29 loc) · 811 Bytes
/
ticker.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
import {EventEmitter} from 'events';
class Ticker extends EventEmitter {
static #timeout = 50;
#totalMs;
#number;
#callback;
constructor(number, callback) {
super();
this.#number = number;
this.#callback = callback;
}
#executeTimer() {
setTimeout(() => {
this.#totalMs += Ticker.#timeout;
this.emit('tick', this.#totalMs);
if (this.#totalMs >= this.#number) {
this.#callback(null, this.#totalMs / Ticker.#timeout);
} else {
this.#executeTimer();
}
}, Ticker.#timeout);
}
start() {
this.#totalMs = 0;
this.#executeTimer();
return this;
}
}
export default (number, callback) => new Ticker(number, callback);