-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazyman.html
More file actions
68 lines (58 loc) · 1.22 KB
/
lazyman.html
File metadata and controls
68 lines (58 loc) · 1.22 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
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LazyMan</title>
</head>
<body>
<script>
function _lazyman(_name){
var _this = this;
_this.tasks = [];
_this.tasks.push(function(){
console.log('Hi! This is ' + _name + '!');
_this.next();
});
setTimeout(function(){
_this.next();
},0);
}
_lazyman.prototype.next = function(){
var _fn = this.tasks.shift();
_fn && _fn();
}
_lazyman.prototype.eat = function(_fruit){
var _this = this;
this.tasks.push(function(){
console.log('Eat ' + _fruit);
_this.next();
});
return _this;
}
_lazyman.prototype.sleep = function(_time){
var _this = this;
_this.tasks.push(function(){
setTimeout(function(){
console.log('Wake up after ' + _time);
_this.next();
},_time);
})
return _this;
}
_lazyman.prototype.sleepFirst = function(_time){
var _this = this;
_this.tasks.unshift(function(){
setTimeout(function(){
console.log('Wake up after ' + _time);
_this.next();
},_time);
})
return _this;
}
var LazyMan = function(_name){
return new _lazyman(_name);
}
LazyMan('TinyZ').eat('milk').sleep(5000).sleepFirst(2000);
</script>
</body>
</html>