-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraffic_light.js
143 lines (109 loc) · 3.82 KB
/
traffic_light.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 1. Wire up the buttons to the lights
// You'll have to select and store the lights as a variable (although it may help you later to have a reference to all of them at once via the 'light' class)
// You'll have to select and store the buttons as separate variables
// then, add an event listener to each of the buttons
// in the 'handler' (the function you give to the listener) you add a class of 'on' to the relevant light
// Also make the lights go on and off on hover (of the light!!)
// 2. (Extra credit): Have a go at making it so that only one light can be on at one time
// 3. (wild&crazy credit) See if you can set up a timer of some sort to do that automatically (You'll have to add new start and stop buttons to the page)
const { log } = console;
document.addEventListener("DOMContentLoaded", () => {
// Turn all lights off function
const allLightsOff = function () {
Array.from(document.getElementsByClassName("light")).forEach((light) => {
light.classList.remove("on");
});
};
// Footer button click
const footerButtonClick = function (e, turnOff = true) {
if (e.target.tagName == "BUTTON") {
if (document.querySelector(`.${e.target.id}`).classList.contains("on")) {
document.querySelector(`.${e.target.id}`).classList.remove("on");
} else {
if (turnOff) {
allLightsOff();
}
document.querySelector(`.${e.target.id}`).classList.add("on");
}
}
};
// Turn light on with button
document.querySelector("footer").addEventListener("click", footerButtonClick);
// Lights on and off on hover
const removeOnClassToLight = function (e) {
e.target.classList.remove("on");
};
const addOnClassToLight = function (e) {
allLightsOff();
e.target.classList.add("on");
};
const addHover = function () {
Array.from(document.getElementsByClassName("light")).forEach((light) => {
light.addEventListener("mouseover", addOnClassToLight);
light.addEventListener("mouseleave", removeOnClassToLight);
});
};
const removeHover = function () {
Array.from(document.getElementsByClassName("light")).forEach((light) => {
light.removeEventListener("mouseover", addOnClassToLight);
light.removeEventListener("mouseleave", removeOnClassToLight);
});
};
addHover();
// AUTOMATIC LIGHTS
let timer;
let i = 0;
const sequence = [
{ duration: 1000, items: ["stop"] },
{ duration: 2000, items: ["stop", "caution"] },
{ duration: 3000, items: ["caution"] },
{ duration: 500, items: [] },
{ duration: 500, items: ["caution"] },
{ duration: 500, items: [] },
{ duration: 500, items: ["caution"] },
{ duration: 500, items: [] },
{ duration: 500, items: ["caution"] },
{ duration: 5000, items: ["go"] },
];
function next() {
allLightsOff();
for (let j = 0; j < sequence[i].items.length; j++) {
footerButtonClick(
{ target: { id: sequence[i].items[j], tagName: "BUTTON" } },
j == 0
);
}
timer = setTimeout(() => {
next();
}, sequence[i].duration);
i++;
if (i >= sequence.length) {
i = 0;
}
}
// Start and Stop buttons
document.querySelector("header").addEventListener("click", (e) => {
if (e.target.id === "start-timer") {
// Clear existing timeout just in case.
clearTimeout(timer);
// Always start sequence from 0
i = 0;
// Remove hover from lights
removeHover();
// Remove listener from footer
document
.querySelector("footer")
.removeEventListener("click", footerButtonClick);
next();
} else {
clearTimeout(timer);
allLightsOff();
// Add hover
addHover();
// Add footer button click
document
.querySelector("footer")
.addEventListener("click", footerButtonClick);
}
});
});