1 | class Timer {
|
2 | constructor (func) {
|
3 | this.func = func;
|
4 | this.running = false;
|
5 | this.id = null;
|
6 |
|
7 | this._handler = () => {
|
8 | this.running = false;
|
9 | this.id = null;
|
10 |
|
11 | return this.func();
|
12 | };
|
13 | }
|
14 |
|
15 | start (timeout) {
|
16 | if (this.running) {
|
17 | clearTimeout(this.id);
|
18 | }
|
19 |
|
20 | this.id = setTimeout(this._handler, timeout);
|
21 | this.running = true;
|
22 | }
|
23 |
|
24 | stop () {
|
25 | if (this.running) {
|
26 | clearTimeout(this.id);
|
27 | this.running = false;
|
28 | this.id = null;
|
29 | }
|
30 | }
|
31 | };
|
32 |
|
33 | Timer.start = (timeout, func) => setTimeout(func, timeout);
|
34 |
|
35 | exports.Timer = Timer;
|