-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
62 lines (54 loc) · 935 Bytes
/
tools.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
61
var Timer=function(func,interval) {
var t=this;
this.tick=function() {
func();
setTimeout(t.tick,interval);
}
this.start=function() {
this.tick();
}
};
var JobQueue=function() {
var running_jobs=0;
var jobs=[];
var concurrency=1;
function start_jobs() {
while(running_jobs<concurrency) {
if(jobs.length==0) {
return;
}
running_jobs++;
var j=jobs.shift();
j(function() {
running_jobs--;
start_jobs();
});
}
}
this.add=function(job) {
jobs.push(job);
start_jobs();
}
this.length=function() {
return jobs.length+running_jobs;
}
this.test=function() {
concurrency=3;
var t=this;
for(i=0;i<10;i++) {
(function(c) {
t.add(function(on_end) {
console.log("task "+c+" start");
setTimeout(function() {
console.log("task "+c+" done");
on_end();
},1000);
});
})(i);
}
}
};
module.exports={
Timer:Timer,
JobQueue:JobQueue,
};