-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemafaro.js
42 lines (33 loc) · 985 Bytes
/
semafaro.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
const img = document.getElementById('img');
const buttons = document.getElementById('buttons');
let colorIndex = 0;
let intervalID = null;
const trafficLight = (event) => {
stopAutomatic();
turnOn[event.target.id]();
}
const nextIndex = () => {
colorIndex = colorIndex < 2 ? ++colorIndex : 0;
/*if (colorIndex < 2) {
colorIndex++
} else {
colorIndex = 0
}*/
}
const changecolor = () => {
const colors = ['red', 'yellow', 'green' ]
const color = colors [ colorIndex ];
turnOn [color] ();
nextIndex ();
}
const stopAutomatic = () => {
clearInterval (intervalID);
}
const turnOn = {
'red': () => img.src = './img/vermelho.png',
'yellow': () => img.src = './img/amarelo.png',
'green': () => img.src = './img/verde.png',
'off': () => img.src = './img/desligado.png',
'automatic': () => intervalID = setInterval( changecolor, 1000 )
}
buttons.addEventListener('click', trafficLight);