-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsemaforo.js
32 lines (25 loc) · 826 Bytes
/
semaforo.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
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;
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',
'automatic': () => intervalId = setInterval( changeColor, 1000 )
}
buttons.addEventListener('click', trafficLight );