-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy-man.js
50 lines (46 loc) · 1.26 KB
/
lazy-man.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
// LazyMan
// 2019-7-15
// author: zhengwei
function lazyMan(name) {
console.log(`Hi I am ${name}`);
const obj = {
task: [],
next() {
// 完成一个任务之后继续完成下面的直到没有任务为止
const task = this.task.shift();
task && task();
},
sleep(second) {
this.task.push(() => {
console.log(`等待${second}秒...`)
setTimeout(() => {
this.next();
}, second * 1000);
})
return this;
},
eat(name) {
this.task.push(() => {
console.log(`I am eating ${name}`)
this.next();
})
return this;
},
sleepFirst(second) {
this.task.unshift(() => {
console.log(`等待${second}秒...`);
setTimeout(() => {
this.next();
}, second * 1000);
})
return this;
}
};
// 先等收集任务
setTimeout(() => {
// 收集好之后开始第一个任务
obj.next();
})
return obj;
}
lazyMan('Tony').eat('lunch').eat('dinner').sleepFirst(5).sleep(10).eat('junk food');