-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (89 loc) · 3.16 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
document.addEventListener("DOMContentLoaded", () => {
let timer; // store the interval reference
let timerValues = {
hour: 0,
min: 0,
sec: 0
}
let isTimerOn = false;
let isPaused = false;
const display = document.getElementById("display");
const inputs = document.getElementById("inputs");
const resetBtn = document.getElementById("reset");
const pause_resumeBtn = document.getElementById("pause-resume");
const hourDisplay = document.getElementById("hour");
const minDisplay = document.getElementById("min");
const secDisplay = document.getElementById("sec");
const hourInp = document.getElementById("hour-inp");
const minInp = document.getElementById("min-inp");
const secInp = document.getElementById("sec-inp");
const startTimerBtn = document.querySelector("#inputs>button");
resetBtn.addEventListener('click', () => {
if (!isTimerOn) return;
setInputsInTimer();
displayTimerValues();
})
pause_resumeBtn.addEventListener('click', () => {
if (!isTimerOn) return;
if (isPaused) {
startTimer();
isPaused = false;
pause_resumeBtn.textContent = "Pause";
} else {
isPaused = true;
clearInterval(timer);
pause_resumeBtn.textContent = "Resume";
}
})
startTimerBtn.addEventListener("click", () => {
setInputsInTimer()
if (timerValues.hour < 0 ||
timerValues.min < 0 ||
timerValues.sec < 0) return;
isTimerOn = true; // starting the timer
startTimer();
toggleDisplay();
})
function setInputsInTimer() {
timerValues.sec = +secInp.value;
timerValues.min = +minInp.value;
timerValues.hour = +hourInp.value;
}
function displayTimerValues() {
hourDisplay.textContent = timerValues.hour;
minDisplay.textContent = timerValues.min;
secDisplay.textContent = timerValues.sec;
}
function startTimer() {
displayTimerValues();
timer = setInterval(() => {
timerValues.sec--;
if (timerValues.sec < 0) {
timerValues.sec = 59;
timerValues.min--;
}
if (timerValues.min < 0) {
timerValues.min = 59;
timerValues.hour--;
}
displayTimerValues()
// Clear the timer after comeback to 00:00:00
if (timerValues.hour < 0 ||
timerValues.min < 0 ||
timerValues.sec < 0) {
clearInterval(timer);
isTimerOn = false;
toggleDisplay();
}
}, 1000)
}
function toggleDisplay() {
if (isTimerOn) {
inputs.style.display = "none";
display.style.display = "block";
} else {
inputs.style.display = "grid";
display.style.display = "none";
}
}
})