-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
98 lines (90 loc) · 1.83 KB
/
code.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
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
const startBtn = document.querySelector("[startBtn]");
const stopBtn = document.querySelector("[stopBtn]");
const timerDisplay = document.querySelector("[timer]");
const pauseBtn = document.querySelector("[pauseBtn]");
timerDisplay.innerHTML = " 00 : 00 : 00 : 00";
let d0 = 0,
d1 = 0,
h0 = 0,
h1 = 0,
c0 = 0,
c1 = 0,
counter0 = 0,
counter1 = 0;
let myInterval;
let intervalSpeed = 1000;
let isTimerRunning = false;
function display() {
return `${d0}${d1} : ${h0}${h1} : ${c0}${c1} : ${counter0}${counter1}`;
}
function updateTimer() {
timerDisplay.textContent = display();
counter1++;
if (counter1 > 9) {
counter1 = 0;
counter0++;
}
if (counter0 > 5) {
counter0 = 0;
c1++;
}
if (c1 > 9) {
c1 = 0;
c0++;
counter1 = 0;
counter0 = 0;
}
if (c0 > 5) {
h1++;
c0 = 0;
c1 = 0;
counter0 = 0;
counter1 = 0;
}
if (h1 > 9 && h0 < 2) {
h1 = 0;
h0++;
c0 = 0;
c1 = 0;
counter0 = 0;
counter1 = 0;
}
if (h0 === 2 && h1 >= 4) {
h0 = 0;
h1 = 0;
d1++;
c0 = 0;
c1 = 0;
counter0 = 0;
counter1 = 0;
}
if (d1 > 9) {
d1 = 0;
d0++;
}
}
function startTimer() {
if (!isTimerRunning) {
myInterval = setInterval(updateTimer, intervalSpeed);
isTimerRunning = true;
}
}
function pauseTimer() {
clearInterval(myInterval);
isTimerRunning = false;
}
function resetTimer() {
clearInterval(myInterval);
d0 = (d1 = h0 = h1 = c0 = c1 = counter0 = counter1 = 0);
timerDisplay.textContent = " 00 : 00 : 00 : 00";
isTimerRunning = false;
}
startBtn.addEventListener("click", () => {
startTimer();
});
pauseBtn.addEventListener("click", () => {
pauseTimer();
});
stopBtn.addEventListener("click", () => {
resetTimer();
});