-
Notifications
You must be signed in to change notification settings - Fork 0
/
链.js
43 lines (37 loc) · 817 Bytes
/
链.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
class LazyManClass {
constructor(name) {
this.taskList = []
this.name = name
setTimeout(() => {
this.next()
}, 0);
}
eat(food) {
this.taskList.push(() => {
console.log(`I am eating ${food}`);
this.next()
})
return this
}
sleep(time) {
this.taskList.push(() => setTimeout(() => {
// console.log(`过去了${time}秒`)
this.next()
}, time * 1000))
return this
}
sleepFirst(time) {
this.taskList.unshift(() => setTimeout(() => {
// console.log(`过去了${time}秒`)
this.next()
}, time * 1000))
return this
}
next() {
// console.log(this.taskList)
let fn = this.taskList.shift()
fn && fn()
}
}
const lazyMan = new LazyManClass('我')
lazyMan.eat('饭').sleepFirst(10).sleep(1).eat('饭')