-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.js
executable file
·60 lines (57 loc) · 1.69 KB
/
threads.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//part of joke.js
// Represents "thread" of concrete object or group of objects
Thread = function () {
};
Thread.prototype = {
"threadPriority": {
'HIGHEST': 10,
'HIGH': 50,
'MEDIUM': 200,
'SLOW': 2000,
'SLOWEST': 2000,
'ALMOST_DEAD': 10000,
'CUSTOM': 100
},
"threadPriopiry": 0,
// Priority of thread, or simply timer Interval
"counter": 0,
// When object ticks "counter" times, then ticking become terminated
"terminated": true,
// determines whether loop is executed or not
"suspended": false,
"ticker": null,
// ticking timer
"tick": function (self, param) { // ticking action
param = self.onTick(self, param);
if (self.counter > 0) {
self.counter--;
this.ticker = setTimeout(self.tick, self.threadPriority.CUSTOM, self, param);
} else {
self.counter = 0;
self.terminate();
self.onEndTicking();
}
},
"start": function (tickParam) {
this.terminated = false;
if (this.ticker)
return false;
this.counter = 4;
var self = this;
this.ticker = setTimeout(this.tick, this.threadPriority.CUSTOM, self, tickParam);
},
"terminate": function () {
if (this.onTerminated) {
this.onTerminated();
}
this.terminated = true;
clearTimeout(this.ticker);
this.ticker = null;
},
// Just example, you should implement this method by himself
"onTick": function (sender, param, scene) {
return param;
},
"onTerminated": function () {
}
};