-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ts
More file actions
47 lines (40 loc) · 1.44 KB
/
Copy pathscript.ts
File metadata and controls
47 lines (40 loc) · 1.44 KB
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
//Enum que define o estado do semáforo
enum TrafficLightState {
Red = 'red',
Yellow = 'Yellow',
Green = 'green',
}
//Obtendo as Divs de Luz
const redLight = document.getElementById('red') as HTMLDivElement;
const yellowLight = document.getElementById('yellow') as HTMLDivElement;
const greenLight = document.getElementById('green') as HTMLDivElement;
//Variavel para armazenar o estado atual do semaforo
let currentState: TrafficLightState = TrafficLightState.Red;
//Funçao para alternar entre as cores do semáforo
function changeLight(){
//Remover a classe 'active' de todas as luzes
redLight.classList.remove('active');
yellowLight.classList.remove('active');
greenLight.classList.remove('active');
// Acender a luz correta com base no estado atual
switch (currentState) {
case TrafficLightState.Red:
redLight.classList.add('active');
currentState = TrafficLightState.Green;
break
case TrafficLightState.Yellow:
yellowLight.classList.add('active');
currentState = TrafficLightState.Red;
break;
case TrafficLightState.Green:
greenLight.classList.add('active');
currentState = TrafficLightState.Yellow;
break;
}
}
//Função que inicia o ciclo do semaforo
function starTrafficLight(){
setInterval(changeLight, 3000);
}
//Iniciar o ciclo do semáforo assim que a pagina carregar
window.onload = starTrafficLight;