-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazyman.html
More file actions
58 lines (57 loc) · 1.15 KB
/
lazyman.html
File metadata and controls
58 lines (57 loc) · 1.15 KB
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
<!DOCTYPE html>
<html>
<head>
<title>a lazyman</title>
</head>
<body>
<script>
function lazyMan(name){
return new lazyman(name);
}
var lazyman = function(name){
this.tasks = [];
var self = this;
var fn = (function(){
return function(){
console.log('hello '+name);
self.next();
}
})(name);
this.tasks.push(fn);
setTimeout(function(){
self.next();
},0);
// console.log(self.tasks);
}
lazyman.prototype.next = function(){
var fn = this.tasks.shift();
fn&&fn();
};
lazyman.prototype.eat = function(food){
var self = this;
var fn = (function(food){
return function(){
console.log('eat '+food);
self.next();
}
})(food);
this.tasks.push(fn);
return this;
};
lazyman.prototype.sleep = function(time){
var self = this;
var fn = (function(time){
return function(){
setTimeout(function(){
console.log('sleep '+time);
self.next();
},time*1000);
}
})(time);
this.tasks.push(fn);
return this;
};
lazyMan('zz').eat('lunch').sleep(3).eat('dinner');
</script>
</body>
</html>