-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (96 loc) · 3.86 KB
/
script.js
File metadata and controls
107 lines (96 loc) · 3.86 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
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
/*cronometro (crescente)*/
const stopwatchDisplay = document.getElementById('stopwatchDisplay');
const startStopwatchBtn = document.getElementById('startStopwatch');
const stopStopwatchBtn = document.getElementById('stopStopwatch');
const resetStopwatchBtn = document.getElementById('resetStopwatch');
let stopwatchInterval;
let stopwatchCentiseconds = 0, stopwatchSeconds = 0, stopwatchMinutes = 0, stopwatchHours = 0;
/*Função para formatar o número (adiciona um 0 à esquerda se for menor que 10)*/
function formatNumber(num) {
return num < 10 ? '0' + num : num;
}
/*atualiza o cronômetro a cada 10 ms*/
function updateStopwatch() {
stopwatchCentiseconds++;
if (stopwatchCentiseconds === 100) {
stopwatchCentiseconds = 0;
stopwatchSeconds++;
}
if (stopwatchSeconds === 60) {
stopwatchSeconds = 0;
stopwatchMinutes++;
}
if (stopwatchMinutes === 60) {
stopwatchMinutes = 0;
stopwatchHours++;
}
stopwatchDisplay.innerHTML =
`${formatNumber(stopwatchHours)}:${formatNumber(stopwatchMinutes)}:${formatNumber(stopwatchSeconds)}<span class="cents">.${formatNumber(stopwatchCentiseconds)}</span>`;
}
startStopwatchBtn.addEventListener('click', () => {
clearInterval(stopwatchInterval);
/*atualiza a cada 10ms para mostrar os centésimos*/
stopwatchInterval = setInterval(updateStopwatch, 10);
});
stopStopwatchBtn.addEventListener('click', () => {
clearInterval(stopwatchInterval);
});
resetStopwatchBtn.addEventListener('click', () => {
clearInterval(stopwatchInterval);
stopwatchCentiseconds = 0;
stopwatchSeconds = 0;
stopwatchMinutes = 0;
stopwatchHours = 0;
stopwatchDisplay.innerHTML = '00:00:00<span class="cents">.00</span>';
});
/*temporizador (decrescente)*/
const countdownDisplay = document.getElementById('countdownDisplay');
const minutesInput = document.getElementById('minutesInput');
const secondsInput = document.getElementById('secondsInput');
const startCountdownBtn = document.getElementById('startCountdown');
const pauseCountdownBtn = document.getElementById('pauseCountdown');
const resetCountdownBtn = document.getElementById('resetCountdown');
let countdownInterval;
let totalSeconds;
/*inicia o temporizador*/
function startCountdown() {
const minutes = parseInt(minutesInput.value) || 0;
const seconds = parseInt(secondsInput.value) || 0;
totalSeconds = (minutes * 60) + seconds;
/*vê se o tempo é valido*/
if (totalSeconds <= 0) {
alert("Por favor, defina um tempo maior que zero.");
return;
}
clearInterval(countdownInterval);
countdownInterval = setInterval(updateCountdown, 1000);
}
/*atualiza o temporizador a cada segundo*/
function updateCountdown() {
if (totalSeconds <= 0) {
clearInterval(countdownInterval);
countdownDisplay.textContent = "Fim!";
return;
}
totalSeconds--;
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
countdownDisplay.textContent = `${formatNumber(minutes)}:${formatNumber(seconds)}`;
}
/*resetar o temporizador*/
function resetCountdown() {
clearInterval(countdownInterval);
const minutes = parseInt(minutesInput.value) || 0;
const seconds = parseInt(secondsInput.value) || 0;
countdownDisplay.textContent = `${formatNumber(minutes)}:${formatNumber(seconds)}`;
}
// Event Listeners dos botões do temporizador
startCountdownBtn.addEventListener('click', startCountdown);
pauseCountdownBtn.addEventListener('click', () => {
clearInterval(countdownInterval);
});
resetCountdownBtn.addEventListener('click', resetCountdown);
/*muda o relogiuo quando o tempo desejado*/
minutesInput.addEventListener('input', resetCountdown);
secondsInput.addEventListener('input', resetCountdown);
resetCountdown();