-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
98 lines (89 loc) · 2.55 KB
/
app.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
let digitaClock = document.querySelector(".digital-clock");
const body = document.querySelector("body"),
hourHand = document.querySelector(".hour"),
minuteHand = document.querySelector(".minute"),
secondHand = document.querySelector(".second"),
modeSwitch = document.querySelector(".mode-switch");
const updateTime = () => {
// Get current time and calculate degrees for clock hands
let date = new Date(),
secToDeg = date.getSeconds() * 6,
minToDeg = date.getMinutes() * 6 + date.getSeconds() * (360 / 3600),
hrToDeg = 30 * date.getHours() + date.getMinutes() / 2;
// Rotate the clock hands to the appropriate degree based on the current time
secondHand.style.transform = `rotate(${secToDeg}deg)`;
minuteHand.style.transform = `rotate(${minToDeg}deg)`;
hourHand.style.transform = `rotate(${hrToDeg}deg)`;
//digital clock
let clock = date.toLocaleTimeString();
digitaClock.innerHTML = clock;
};
// call updateTime to set clock hands every second
setInterval(updateTime, 1000);
//call updateTime function on page load
updateTime();
//stopwatch
let hour = 0;
let min = 0;
let sec = 0;
let count = 0;
let start_stop = document.querySelector("#check");
start_stop.addEventListener("click", (e) => {
if (start_stop.checked) {
timer = true;
stopwatch();
} else {
timer = false;
}
});
let reset = document.querySelector(".reset");
reset.addEventListener("click", (e) => {
timer = false;
hour = 0;
min = 0;
sec = 0;
count = 0;
start_stop.checked = false;
document.querySelector(".stop-hour").innerText = "00";
document.querySelector(".stop-min").innerText = "00";
document.querySelector(".stop-sec").innerText = "00";
document.querySelector(".stop-ms").innerText = "00";
});
function stopwatch() {
if (timer) {
count++;
if (count == 100) {
sec++;
count = 0;
}
if (sec == 60) {
min++;
sec = 0;
}
if (min == 60) {
hour++;
min = 0;
}
let hrString = hour;
let minString = min;
let secString = sec;
let countString = count;
if (hour < 10) {
hrString = "0" + hrString;
}
if (min < 10) {
minString = "0" + minString;
}
if (sec < 10) {
secString = "0" + secString;
}
if (count < 10) {
countString = "0" + countString;
}
document.querySelector(".stop-hour").innerText = hrString;
document.querySelector(".stop-min").innerText = minString;
document.querySelector(".stop-sec").innerText = secString;
document.querySelector(".stop-ms").innerText = countString;
setTimeout(stopwatch, 10);
}
}