-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (56 loc) · 1.95 KB
/
app.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
document.addEventListener('DOMContentLoaded', () => {
// Elements
const chronometer = document.querySelector('#chronometer');
const btnStart = document.querySelector('#btn-start-stop');
const btnRestart = document.querySelector('#btn-restart');
// Events
btnStart.addEventListener('click', (e) => {
if (chronoStatus === 'paused') {
chronoStatus = "started";
interval = window.setInterval(() => {
chronometer.innerText = updateChronometer()
}, 1000);
btnStart.innerHTML = '<i class="bi bi-pause-fill"></i>';
btnStart.classList.remove('start');
btnStart.classList.add('paused');
} else {
chronoStatus = "paused";
window.clearInterval(interval);
btnStart.innerHTML = '<i class="bi bi-play-fill"></i>';
btnStart.classList.remove('paused');
btnStart.classList.add('start');
}
});
btnRestart.addEventListener('click', (e) => {
window.clearInterval(interval);
chronoStatus = "paused";
seconds = 0;
minutes = 0;
hours = 0;
chronometer.innerText = "00:00:00";
btnStart.classList.remove('paused');
btnStart.classList.add('start');
btnStart.innerHTML = '<i class="bi bi-play-fill"></i>';
});
});
let [hours, minutes, seconds] = [0, 0, 0];
let interval;
let chronoStatus = "paused";
function updateChronometer() {
seconds++;
if (seconds / 60 === 1) {
seconds = 0;
minutes++;
if (minutes / 60 === 1) {
minutes = 0;
hours++;
}
}
const secondsFormat = format(seconds);
const minutesFormat = format(minutes);
const hoursFormat = format(hours);
return `${hoursFormat}:${minutesFormat}:${secondsFormat}`
}
function format(unit) {
return unit < 10 ? '0' + unit : unit;
}