-
Notifications
You must be signed in to change notification settings - Fork 38
/
lazyman.js
48 lines (42 loc) · 972 Bytes
/
lazyman.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
export function LazyMan(name) {
return new LazyManFactory(name);
}
function LazyManFactory(name) {
this.name = name;
this.flag = false;
this.tasks = [];
this.tasks.push(() => {
console.log('hi,', this.name);
this.next();
});
setTimeout(() => {
this.next();
}, 0)
}
LazyManFactory.prototype.eat = function (sth) {
this.tasks.push(() => {
console.log(this.name, ' eat dinner');
this.next();
});
return this;
}
LazyManFactory.prototype.next = function () {
let fn = this.tasks.shift();
fn && fn();
}
LazyManFactory.prototype.sleep = function (delay) {
this.tasks.push(() => {
setTimeout(() => {
this.next();
}, delay * 1000);
});
return this;
}
LazyManFactory.prototype.sleepFirst = function (delay) {
this.tasks.unshift(() => {
setTimeout(() => {
this.next();
}, delay * 1000);
});
return this;
}