forked from cri-2014/1-lights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrafficlight.htm
119 lines (105 loc) · 2.65 KB
/
trafficlight.htm
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!doctype html>
<html>
<head>
<title>
Ñâåòîôîð
</title>
</head>
<body>
<script type="text/javascript">
function TrafficLight(config) {
this.greenTime = config.greenTime;
this.yellowTime = config.yellowTime;
this.redTime = config.redTime;
this.timeoutID = null;
this.switchDate = null;
this._state = null;
this.tram = false;
TrafficLight.prototype.state = function() {
return (this.tram)?"green":this._state;
};
TrafficLight.prototype.toRed = function() {
clearTimeout(this.timeoutID);
this._state = "red";
this.switchDate = new Date();
this.timeoutID = setTimeout(function() {this.toGreen();}.bind(this), this.redTime*1000);
};
TrafficLight.prototype.toGreen = function() {
clearTimeout(this.timeoutID);
this._state = "green";
this.switchDate = new Date();
this.timeoutID = setTimeout(function() {this.toYellow();}.bind(this), this.greenTime*1000);
};
TrafficLight.prototype.toYellow = function() {
clearTimeout(this.timeoutID);
this._state = "yellow";
this.switchDate = new Date();
this.timeoutID = setTimeout(function() {this.toRed();}.bind(this), this.yellowTime*1000);
};
TrafficLight.prototype.letTramGo = function() {
setTimeout(function() {
this.tram = true;
setTimeout(function() {
this.tram = false;
if (this.switchDate - new Date() > 0.8*this[this._state + "Time"]) {
switch (this._state) {
case "green":
this.toYellow();
break;
case "yellow":
this.toRed();
break;
case "red":
this.toGreen();
break;
}
}
}.bind(this),10000);
}.bind(this),3000);
}
TrafficLight.prototype.subscribeTram = function(event) {
event.addListener(function() {this.letTramGo();}.bind(this));
}
}
function tramEvent() {
this.listeners=[];
tramEvent.prototype.addListener = function(func) {
this.listeners.push(func);
}
tramEvent.prototype.removeListener = function(func) {
for(var i=0; i<this.listeners.length ; i++) {
if (this.listeners[i] === func) {
this.listeners.splice(i,1);
}
}
}
tramEvent.prototype.emit = function() {
for(var i=0; i<this.listeners.length ; i++) {
setTimeout(this.listeners[i],0);
}
}
}
var config = {
greenTime: 6,
yellowTime: 2,
redTime: 6
}
var trafficLight = new TrafficLight(config);
var tram = new tramEvent();
trafficLight.subscribeTram(tram);
trafficLight.toGreen();
/*setInterval(function() {alert("now is " + trafficLight.state());}, 2000);
setTimeout(function() {
tram.emit();
alert("tram is coming...");
setTimeout(function() {
alert("tram!");
},3000);
setTimeout(function() {
alert("tram passed");
},13000);
}, 10000);
*/
</script>
</body>
</html>