-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (56 loc) · 1.24 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
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
let timerInterval;
let seconds = 0;
let minutes = 0;
let hours = 0;
let timerText = document.getElementById('timer');
const getTimerText = () => {
let text = "";
if (hours < 10) {
text = text.concat("0", hours, ":");
} else {
text = text.concat(hours, ":");
}
if (minutes < 10) {
text = text.concat("0", minutes, ":");
} else {
text = text.concat(minutes, ":");
}
if (seconds < 10) {
text = text.concat("0", seconds);
} else {
text = text.concat(seconds);
}
return text;
}
const updateTimerUI = () => {
timerText.textContent = getTimerText();
}
let startBtn = document.getElementById('start-btn');
startBtn.onclick = () => {
// Clear the timer interval first
clearInterval(timerInterval);
timerInterval = setInterval(() => {
seconds++;
if (seconds === 60) {
minutes++;
seconds = 0;
if (minutes === 60) {
hours++;
minutes = 0;
}
}
updateTimerUI();
}, 1000);
}
let stopBtn = document.getElementById('stop-btn');
stopBtn.onclick = () => {
clearInterval(timerInterval);
}
let resetBtn = document.getElementById('reset-btn');
resetBtn.onclick = () => {
clearInterval(timerInterval);
seconds = 0;
minutes = 0;
hours = 0;
updateTimerUI();
}