-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
100 lines (90 loc) · 2.64 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
99
100
const updateDateAndTime = () => {
const now = new Date();
const dateOptions = {
weekday: "short",
year: "numeric",
month: "short",
day: "numeric",
};
const timeOptions = {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: true,
};
const time = now.toLocaleTimeString();
// const date = now.toLocaleDateString(); // mm/dd/yyyy
const date = now.toLocaleDateString(undefined, dateOptions);
document.getElementById("dateDay").innerHTML = date;
document.getElementById("content").innerHTML = time;
};
setInterval(updateDateAndTime, 1000);
const stpWtchTime = document.getElementById("display");
const displayStopValue = document.querySelector(".displayTime");
const strtBtn = document.querySelector(".strtBtn");
let intervalId;
let hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0;
const setTime = (hours, minutes, seconds, milliseconds) => {
const set = (num) => (num < 10 ? `0${num}` : num);
return `${set(hours)}:${set(minutes)}:${set(seconds)}:${set(milliseconds)}`;
};
let stpWatch = false;
const strtTimer = () => {
if (!stpWatch) {
stpWatch = true;
intervalId = setInterval(() => {
milliseconds++;
if (milliseconds === 100) {
milliseconds = 0;
seconds++;
}
// seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
}
if (minutes === 60) {
minutes = 0;
hours++;
}
stpWtchTime.innerText = setTime(hours, minutes, seconds, milliseconds);
}, 10);
}
// strtBtn.innerText = "Pause"
// console.log(strtTime);
};
strtBtn.addEventListener("click", strtTimer);
const stpTime = () => {
clearInterval(intervalId);
stpWatch = false;
};
document.querySelector(".stpBtn").addEventListener("click", stpTime);
const rstTime = () => {
clearInterval(intervalId);
stpWatch = false;
hours = 0;
minutes = 0;
seconds = 0;
milliseconds = 0;
stpWtchTime.innerText = setTime(hours, minutes, seconds, milliseconds);
};
document.querySelector(".rstBtn").addEventListener("click", rstTime);
const displayTime = () => {
const displayTime = document.createElement("p");
displayTime.classList.contains("displayPara");
displayTime.innerText = `The time is ${setTime(
hours,
minutes,
seconds,
milliseconds
)}`;
displayStopValue.append(displayTime);
};
document.querySelector(".shwBtn").addEventListener("click", displayTime);
const clrDisplayTime = () => {
displayStopValue.innerHTML = "";
};
document.querySelector(".clrBtn").addEventListener("click", clrDisplayTime);