Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var northBound = document.getElementById("northBound");
- var southBound = document.getElementById("southBound");
- var eastBound = document.getElementById("eastBound");
- var westBound = document.getElementById("westBound");
- var signalGreen = "light-green";
- var signalYellow = "light-yellow";
- var signalRed = "light-red";
- var greenTimer = 10000;
- var yellowTimer = 3000;
- var stopSimulation = false;
- // Lights work on two groups
- // North and South are one group
- // East and West are another group
- // Only one group can be active at a time
- var norSouActive = false;
- function startSimulation() {
- console.log('DEBUG: Triggered start simulation');
- setupSimulation();
- };
- function stopSimulation() {
- stopSimulation = true;
- console.log('stopping simulation...');
- };
- function setupSimulation() {
- // North and South traffic go first
- norSouActive = true;
- console.log('initializing norSouGroup to green');
- switchNorthSouthGroup('g');
- // Ensure West and East are stopped
- console.log('initializing easWesGroup to red');
- switchEastWestGroup('r');
- runSimulation();
- };
- function runSimulation() {
- console.log('prepareForChange timer...');
- setTimeout(prepareForChange, greenTimer);
- console.log('changeGroup...');
- changeGroup();
- console.log('runSimulation');
- runSimulation();
- };
- function prepareForChange() {
- if(norSouActive) {
- switchNorthSouthGroup('y');
- setTimeout(changeGroup(), yellowTimer);
- } else {
- switchEastWestGroup('y');
- setTimeout(changeGroup(), yellowTimer);
- }
- };
- function changeGroup() {
- if(norSouActive) {
- switchNorthSouthGroup('r');
- switchEastWestGroup('g');
- } else {
- switchEastWestGroup('r');
- switchNorthSouthGroup('g');
- }
- };
- // g - green y - yellow r - red
- function switchEastWestGroup(status) {
- switch(status) {
- case "g":
- westBound.className = "grid-item " + signalGreen;
- eastBound.className = "grid-item " + signalGreen;
- break;
- case "y":
- westBound.className = "grid-item " + signalYellow;
- eastBound.className = "grid-item " + signalYellow;
- break;
- case "r":
- westBound.className = "grid-item " + signalRed;
- eastBound.className = "grid-item " + signalRed;
- break;
- }
- };
- // g - green y - yellow r - red
- function switchNorthSouthGroup(status) {
- switch(status) {
- case "g":
- northBound.className = "grid-item " + signalGreen;
- southBound.className = "grid-item " + signalGreen;
- break;
- case "y":
- northBound.className = "grid-item " + signalYellow;
- southBound.className = "grid-item " + signalYellow;
- break;
- case "r":
- northBound.className = "grid-item " + signalRed;
- southBound.className = "grid-item " + signalRed;
- break;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement