-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
146 lines (129 loc) · 4.12 KB
/
app.js
File metadata and controls
146 lines (129 loc) · 4.12 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//GETTING ELEMENTS HERE
var hoursInput = document.getElementById("typeHours");
var minutesInput = document.getElementById("typeMinutes");
var secondsInput = document.getElementById("typeSeconds");
var selectPara = document.getElementById("selectPara");
var inputDiv = document.getElementById("inputDiv");
var timerDiv = document.getElementById("timerDiv");
var timerHours = document.getElementById("timerHours");
var timerMinutes = document.getElementById("timerMinutes");
var timerSeconds = document.getElementById("timerSeconds");
var startBtn2 = document.getElementById("startBtn2");
var pauseBtnId = document.getElementById("pauseBtnId");
// DEFINING VARIABLES HERE
var setIntervalFunc;
var intervalHours;
var intervalMinutes;
var intervalSeconds;
// console.log(intervalSeconds);
//WRITING FUNCTIONS HERE
function timerFunction() {
// console.log(intervalSeconds);
if (intervalSeconds > 0) {
intervalSeconds--;
timerSeconds.innerHTML = intervalSeconds;
secondsInput.value = intervalSeconds;
// console.log("Seconds Value:", intervalSeconds);
} else if (intervalMinutes > 0 && intervalSeconds === 0) {
intervalMinutes--;
intervalSeconds = 59;
timerSeconds.innerHTML = intervalSeconds;
secondsInput.value = intervalSeconds;
timerMinutes.innerHTML = intervalMinutes;
minutesInput.value = intervalMinutes;
// console.log("Minutes Value:", intervalSeconds);
} else if (
intervalHours > 0 &&
intervalMinutes === 0 &&
intervalSeconds === 0
) {
intervalHours--;
intervalMinutes = 59;
intervalSeconds = 59;
timerHours.innerHTML = intervalHours;
hoursInput.value = intervalHours;
timerSeconds.innerHTML = intervalSeconds;
secondsInput.value = intervalSeconds;
timerMinutes.innerHTML = intervalMinutes;
minutesInput.value = intervalMinutes;
// console.log("Hours Value:", intervalHours);
} else {
resetBtn();
// console.log("Countdown finished.");
Swal.fire({
icon: "success",
title: "Done",
text: "Timer completed!",
});
}
}
function startBtn() {
if (hoursInput.value >= 0 && hoursInput.value < 24) {
if (minutesInput.value >= 0 && minutesInput.value < 60) {
if (secondsInput.value >= 0 && secondsInput.value < 60) {
if (
hoursInput.value !== "0" ||
minutesInput.value !== "0" ||
secondsInput.value !== "0"
) {
selectPara.style.display = "none";
inputDiv.style.display = "none";
timerDiv.style.visibility = "visible";
startBtn2.style.display = "none";
pauseBtnId.style.display = "block";
timerHours.innerHTML = hoursInput.value;
timerMinutes.innerHTML = minutesInput.value;
timerSeconds.innerHTML = secondsInput.value;
intervalHours = Number(hoursInput.value);
intervalMinutes = Number(minutesInput.value);
intervalSeconds = Number(secondsInput.value);
startTimer();
} else {
Swal.fire({
icon: "warning",
title: "Error",
text: "You must specify Hours or Minutes or Seconds to start the timer!",
});
}
} else {
Swal.fire({
icon: "warning",
title: "Error",
text: "seconds must be less 60 and not negative",
});
}
} else {
Swal.fire({
icon: "warning",
title: "Error",
text: "minutes must be less 60 and not negative",
});
}
} else {
Swal.fire({
icon: "warning",
title: "Error",
text: "hours must be less 24 and not negative",
});
}
}
function startTimer() {
setIntervalFunc = setInterval(timerFunction, 1000);
}
function resetBtn() {
clearInterval(setIntervalFunc);
timerHours.innerHTML = 0;
timerMinutes.innerHTML = 0;
timerSeconds.innerHTML = 0;
hoursInput.value = 0;
minutesInput.value = 0;
secondsInput.value = 0;
selectPara.style.display = "block";
inputDiv.style.display = "block";
timerDiv.style.visibility = "hidden";
}
function pauseBtn() {
clearInterval(setIntervalFunc);
startBtn2.style.display = "block";
pauseBtnId.style.display = "none";
}