-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLazyMan.js
64 lines (52 loc) · 1.25 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class C {
constructor (name) {
this.name = name;
this.tasks = [];
var self = this;
this.tasks.push(function() {
console.log('Hi, this is ' + self.name);
self.next();
});
setTimeout(()=>this.next(), 0);
}
next () {
let fn = this.tasks.shift();
fn && fn();
}
eat (sm) {
var self = this;
let fn = function () {
setTimeout(function () {
console.log('Eat ' + sm);
}, 0);
self.next();
};
this.tasks.push(fn);
return this;
}
sleep (t) {
var self = this;
let fn = function () {
setTimeout(function () {
console.log('Sleep ' + t + ' seconds...');
self.next();
}, t*1000);
};
this.tasks.push(fn);
return this;
}
sleepFirst (t) {
var self = this;
let fn = function () {
setTimeout(function () {
console.log('First Sleep ' + t + ' seconds...');
self.next();
}, t*1000);
};
this.tasks.unshift(fn);
return this;
}
}
const LazyMan = function(name) {
return new C(name);
}