/
Timer.ts
36 lines (32 loc) · 912 Bytes
/
Timer.ts
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
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
export class Timer {
public running = false;
public id: any;
public _handler: any;
constructor(public func) {
this.id = null;
this._handler = () => {
this.running = false;
this.id = null;
return this.func();
};
}
public start(timeout) {
if (this.running) { clearTimeout(this.id); }
this.id = setTimeout(this._handler, timeout);
return this.running = true;
}
public stop() {
if (this.running) {
clearTimeout(this.id);
this.running = false; return this.id = null;
}
}
public static start (timeout, func) {
setTimeout(func, timeout);
}
}