-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardman.js
69 lines (63 loc) · 1.31 KB
/
hardman.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
62
63
64
65
66
67
68
69
class _HardMan {
constructor(name) {
this.taskQueue = [];
this.taskQueue.push(() => {
setTimeout(() => {
this.next();
});
});
this.taskQueue.push(() => {
console.log(`I am ${name}`);
this.next();
});
this.next();
}
next() {
let task = this.taskQueue.shift();
task && task();
}
learn(lesson) {
this.taskQueue.push(() => {
console.log(`Learning ${lesson}`);
this.next();
});
return this;
}
rest(sec) {
this.taskQueue.push(() => {
setTimeout(() => {
console.log(`等待${sec}秒..`);
console.log(`Start learning after ${sec} seconds`);
this.next();
}, sec * 1000);
});
return this;
}
restFirst(sec) {
this.taskQueue.unshift(() => {
setTimeout(() => {
console.log(`等待${sec}秒..`);
console.log(`Start learning after ${sec} seconds`);
this.next();
}, sec * 1000);
});
return this;
}
}
const HardMan = function (name) {
return new _HardMan(name);
};
HardMan("jack")
.restFirst(3)
.learn("Chinese")
.learn("Englsih")
.rest(2)
.learn("Japanese");
// //等待3秒..
// Start learning after 3 seconds
// I am jack
// Learning Chinese
// Learning Englsih
// //等待2秒..
// Start learning after 2 seconds
// Learning Japanese