-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
176 lines (149 loc) · 5.73 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// variables for HTML elements
let clock = document.getElementById("clock");
let stopwatch = document.getElementById("stopwatch");
let countdown = document.getElementById("countdown");
let inhour = document.getElementById("inhour");
let inmin = document.getElementById("inmin");
let insec = document.getElementById("insec");
let lapbtn = document.getElementById("lapbtn");
let playbtn = document.getElementById("play-pausebtn");
let stopbtn = document.getElementById("stopbtn");
let inputs = document.querySelectorAll('.box');
let btndiv = document.querySelector('.btndiv');
let lapdiv = document.querySelector('.lap-div');
let laplen = document.getElementsByClassName('new-lap');
//app variable for keeping track of current working app
let app = 'Clock';
//Initializing variables for counting clock time
let clkhr = 0;
let clkmin = 0;
let clksec = 0;
//Initializing variables for counting stopwatch time
let swhr = 0;
let swmin = 0;
let swsec = 0;
//Initializing variables for counting countdown time
let cdhr = 0;
let cdmin = 0;
let cdsec = 0;
//Initializing timeout variables
let swtimeout;
let cdtimeout;
//Clock will be selected default
checkApp(stopwatch, countdown, clock);
setInterval(clockTime, 0);
//selecting app to display
clock.onclick = () => { checkApp(stopwatch, countdown, clock); }
stopwatch.onclick = () => { checkApp(clock, countdown, stopwatch); }
countdown.onclick = () => { checkApp(clock, stopwatch, countdown); }
playbtn.onclick = () => {
if (app == 'StopWatch') { //if stopwatch app is selected
if (playbtn.className == "fa-solid fa-play") {
playbtn.className = "fa-solid fa-pause";
swtimeout = setInterval(swTime, 1000);
} else {
playbtn.className = "fa-solid fa-play";
clearInterval(swtimeout);
}
} else { //if countdown app is selected
if (playbtn.className == "fa-solid fa-play") {
block();
playbtn.className = "fa-solid fa-pause";
cdhr = parseInt(inhour.value);
cdmin = parseInt(inmin.value);
cdsec = parseInt(insec.value);
cdtimeout = setInterval(cdTime, 1000);
} else {
unblock();
playbtn.className = "fa-solid fa-play";
clearInterval(cdtimeout);
}
}
}
lapbtn.onclick = () => {
let newlap = document.createElement('div');
let laptime = document.createElement('span');
let trash = document.createElement('i');
newlap.className = 'new-lap';
laptime.className = 'laptime';
trash.className = 'fa-solid fa-delete-left';
trash.onclick = () => { newlap.remove(); }
let hr = swhr < 10 ? '0' + swhr : swhr;
let min = swmin < 10 ? '0' + swmin : swmin;
let sec = swsec < 10 ? '0' + swsec : swsec;
laptime.innerHTML = hr + " : " + min + " : " + sec;
newlap.appendChild(laptime)
newlap.appendChild(trash);
lapdiv.appendChild(newlap);
}
stopbtn.onclick = resetclk; //function declared explicitly to
function clockTime() {
app == 'StopWatch' ? laplen.length == 0 ? lapdiv.style.display = 'none' : lapdiv.style.display = 'flex' : 0;
let today = new Date();
clkhr = today.getHours();
clkmin = today.getMinutes();
clksec = today.getSeconds();
app == 'Clock' ? Display(clkhr, clkmin, clksec) : 0;
}
function swTime() {
swsec++;
if (swsec == 60) swmin += 1, swsec = 0;
if (swmin == 60) swhr += 1, swmin = 0;
app == 'StopWatch' ? Display(swhr, swmin, swsec) : 0;
}
function cdTime() {
if (cdhr == 0 && cdmin == 0 && cdsec == 0) {
unblock();
resetclk();
} else {
if (cdsec != 0) cdsec--;
else if (cdmin != 0 && cdsec == 0) cdmin--, cdsec = 59;
else if (cdhr != 0 && cdmin == 0 && cdsec == 0) cdhr--, cdmin = 59, cdsec = 59;
app == 'CountDown' ? Display(cdhr, cdmin, cdsec) : 0;
}
}
function block() {
inputs.forEach(element => element.readOnly = true);
inputs.forEach(element => element.style.cursor = 'default');
}
function unblock() {
inputs.forEach(element => element.readOnly = false);
}
function Display(e1, e2, e3) {
inhour.value = e1 < 10 ? '0' + e1 : e1;
inmin.value = e2 < 10 ? '0' + e2 : e2;
insec.value = e3 < 10 ? '0' + e3 : e3;
}
function clearDisplay() {
inhour.value = '00';
inmin.value = '00';
insec.value = '00';
}
function resetclk() {
playbtn.className = "fa-solid fa-play";
if (app == 'StopWatch') {
swhr = 0, swmin = 0, swsec = 0;
clearInterval(swtimeout);
} else {
clearInterval(cdtimeout);
alert("CountDown Finished !");
}
clearDisplay();
}
function checkApp(a1, a2, a3) {
// choosing currently selected app
a3.className == 'Selected' ? 0 : clearDisplay();
a1.className == 'Selected' ? a1.classList.toggle('Selected') : 0;
a2.className == 'Selected' ? a2.classList.toggle('Selected') : 0;
a3.className != 'Selected' ? a3.classList.toggle('Selected') : 0;
app = a3.innerText;
// displaying content according to selected app
app == 'CountDown' ? unblock() : block();
app == 'Clock' ? btndiv.style.display = 'none' : btndiv.style.display = 'flex';
app == 'Clock' || app == 'CountDown' ? lapdiv.style.display = 'none' : lapdiv.style.display = 'flex';
app == 'CountDown' ? lapbtn.style.display = 'none' : lapbtn.style.display = 'inline-block';
// changing coontrol buttons according to selected app
if (app == 'StopWatch' && swhr == 0 && swmin == 0 && swsec == 0) playbtn.className = "fa-solid fa-play";
else if (app == 'CountDown' && cdhr == 0 && cdmin == 0 && cdsec == 0) playbtn.className = "fa-solid fa-play";
else if (swhr != 0 || swmin != 0 || swsec != 0 || cdhr != 0 || cdmin != 0 || cdsec != 0) playbtn.className = "fa-solid fa-pause";
}