-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (44 loc) · 1.09 KB
/
index.js
File metadata and controls
53 lines (44 loc) · 1.09 KB
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
class Schedule {
start(func, ms) {
if (this.exists()) { this.clear(); }
this.timer = setTimeout(func, ms);
this.end = this.timer._idleTimeout / 1000;
}
exists() {
return this.timer ? true : false;
}
completeness() {
return this.elapsed() / this.end;
}
elapsed() {
if (!this.timer) return -1;
return process.uptime() - this.timer._idleStart / 1000;
}
left() {
if (!this.timer) return -1;
return (this.timer._idleStart + this.timer._idleTimeout) / 1000 - process.uptime();
}
clear() {
if (!this.timer) return;
clearTimeout(this.timer);
delete this.timer;
delete this.end;
}
}
class Multiple {
start(func, ms) {
if (this.exists()) { this.clear(); }
this.timer = setInterval(func, ms);
}
exists() {
return this.timer ? true : false;
}
clear() {
if (!this.timer) return;
clearInterval(this.timer);
delete this.timer;
}
}
module.exports = {
Schedule: Schedule, Multiple: Multiple
};