-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (36 loc) · 1.01 KB
/
script.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
let timerInterval;
let running = false;
let totalSeconds = 0;
/*Inicação do cronometro contando os segundos */
function startTimer() {
if (!running) {
running = true;
timerInterval = setInterval(updateTimer, 1000);
}
}
/*Para a contagem */
function stopTimer() {
running = false;
clearInterval(timerInterval);
}
/*Reseta */
function resetTimer() {
stopTimer();
totalSeconds = 0;
updateTimer();
}
/*Calcular e atualizar o tempo */
function updateTimer() {
totalSeconds++;
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
const hoursFormatted = formatTime(hours);
const minutesFormatted = formatTime(minutes);
const secondsFormatted = formatTime(seconds);
document.getElementById('timer').textContent = `${hoursFormatted}:${minutesFormatted}:${secondsFormatted}`;
}
/*Formatação do tempo */
function formatTime(time) {
return time < 10 ? `0${time}` : time;
}