-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcdpw.js
More file actions
82 lines (57 loc) · 1.46 KB
/
cdpw.js
File metadata and controls
82 lines (57 loc) · 1.46 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Cooldown Process Wait
class CDPW {
constructor() {
// Set Wait process
this.wp = [];
this.pID = false;
this.globalID = 0;
}
add(callback, time) {
var wp = new WP({
callback,
time,
id: ++this.globalID
});
this.wp.push(wp);
return wp.id;
}
remove(id) {
return this.wp.splice(this.wp.findIndex(wp => wp.id == id), 1);
}
executer() {
var self = this;
this.wp.forEach((procc, id) => {
if(_.now() - procc.timeStart >= procc.timeWait ) {
try {
procc.callback();
} catch(e) { }
_.con("Execute CB ID: "+procc.id);
// self.wp.remove(id);
self.remove(procc.id);
}
});
return this;
}
startExecute(timer=5) {
var self = this;
if(this.pID) clearInterval(this.pID);
this.pID = setInterval(() => {
self.executer();
}, timer*1e2);
}
}
// Wait process
class WP {
constructor({ callback, time = 30, id = 0 } = {}) {
this.callback = callback;
this.timeWait = time;
this.timeStart = _.now();
this.id = id;
}
}
module.exports = CDPW;
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};