Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home work: traffic light + tram #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions trafficlight.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это пять! 👍

};

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>