-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
227 lines (201 loc) · 6.14 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
function updateClock() {
const now = new Date();
const clock = document.querySelector("#clock .clock");
const dateElement = document.querySelector("#clock .date");
const timezoneSelect = document.getElementById("timezoneSelect");
const selectedTimezone = timezoneSelect.value;
let displayTime;
if (selectedTimezone === "local") {
displayTime = now;
} else {
displayTime = new Date(
now.toLocaleString("en-US", { timeZone: selectedTimezone })
);
}
const hours = String(displayTime.getHours()).padStart(2, "0");
const minutes = String(displayTime.getMinutes()).padStart(2, "0");
const seconds = String(displayTime.getSeconds()).padStart(2, "0");
clock.innerHTML = `${hours}:${minutes}<span class="pulse">:</span>${seconds}`;
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
dateElement.textContent = `${days[displayTime.getDay()]}, ${
months[displayTime.getMonth()]
} ${displayTime.getDate()}`;
}
setInterval(updateClock, 1000);
updateClock();
document
.getElementById("timezoneSelect")
.addEventListener("change", updateClock);
const tabs = document.querySelectorAll(".tab");
const contents = document.querySelectorAll(".content");
tabs.forEach((tab) => {
tab.addEventListener("click", () => {
const target = tab.getAttribute("data-tab");
tabs.forEach((t) => t.classList.remove("active"));
contents.forEach((c) => c.classList.remove("active"));
tab.classList.add("active");
document.getElementById(target).classList.add("active");
});
});
const alarmTimeInput = document.getElementById("alarmTime");
const setAlarmButton = document.getElementById("setAlarm");
const alarmStatus = document.getElementById("alarmStatus");
let alarmTimeout;
setAlarmButton.addEventListener("click", () => {
const alarmTime = alarmTimeInput.value;
if (alarmTime) {
const [hours, minutes] = alarmTime.split(":");
const now = new Date();
const alarm = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
hours,
minutes
);
if (alarm > now) {
const timeToAlarm = alarm - now;
alarmStatus.textContent = "Alarm set";
alarmTimeout = setTimeout(() => {
alert("Alarm!");
alarmStatus.textContent = "";
}, timeToAlarm);
} else {
alarmStatus.textContent = "Please set a future time";
}
} else {
alarmStatus.textContent = "Please set a valid time";
}
});
const stopwatchDisplay = document.getElementById("stopwatchDisplay");
const startStopwatchButton = document.getElementById("startStopwatch");
const resetStopwatchButton = document.getElementById("resetStopwatch");
let stopwatchInterval;
let stopwatchTime = 0;
function updateStopwatch() {
const hours = Math.floor(stopwatchTime / 3600)
.toString()
.padStart(2, "0");
const minutes = Math.floor((stopwatchTime % 3600) / 60)
.toString()
.padStart(2, "0");
const seconds = (stopwatchTime % 60).toString().padStart(2, "0");
stopwatchDisplay.textContent = `${hours}:${minutes}:${seconds}`;
}
startStopwatchButton.addEventListener("click", () => {
if (startStopwatchButton.textContent === "Start") {
stopwatchInterval = setInterval(() => {
stopwatchTime++;
updateStopwatch();
}, 1000);
startStopwatchButton.textContent = "Stop";
} else {
clearInterval(stopwatchInterval);
startStopwatchButton.textContent = "Start";
}
});
resetStopwatchButton.addEventListener("click", () => {
clearInterval(stopwatchInterval);
stopwatchTime = 0;
updateStopwatch();
startStopwatchButton.textContent = "Start";
});
const timerMinutesInput = document.getElementById("timerMinutes");
const timerDisplay = document.getElementById("timerDisplay");
const startTimerButton = document.getElementById("startTimer");
const resetTimerButton = document.getElementById("resetTimer");
let timerInterval;
let timerTime = 0;
function updateTimer() {
const minutes = Math.floor(timerTime / 60)
.toString()
.padStart(2, "0");
const seconds = (timerTime % 60).toString().padStart(2, "0");
timerDisplay.textContent = `${minutes}:${seconds}`;
if (timerTime === 0) {
clearInterval(timerInterval);
alert("Timer finished!");
startTimerButton.textContent = "Start";
}
}
startTimerButton.addEventListener("click", () => {
if (startTimerButton.textContent === "Start") {
const minutes = parseInt(timerMinutesInput.value);
if (isNaN(minutes) || minutes <= 0) {
alert("Please enter a valid number of minutes");
return;
}
timerTime = minutes * 60;
timerInterval = setInterval(() => {
timerTime--;
updateTimer();
}, 1000);
startTimerButton.textContent = "Stop";
} else {
clearInterval(timerInterval);
startTimerButton.textContent = "Start";
}
});
resetTimerButton.addEventListener("click", () => {
clearInterval(timerInterval);
timerTime = 0;
updateTimer();
startTimerButton.textContent = "Start";
});
const alarmSound = document.getElementById("alarmSound");
setAlarmButton.addEventListener("click", () => {
const alarmTime = alarmTimeInput.value;
if (alarmTime) {
const [hours, minutes] = alarmTime.split(":");
const now = new Date();
const alarm = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
hours,
minutes
);
if (alarm > now) {
const timeToAlarm = alarm - now;
alarmStatus.textContent = "Alarm set";
alarmTimeout = setTimeout(() => {
alarmSound.play().catch((error) => {
console.error("Audio playback failed:", error);
});
alert("Alarm!");
alarmStatus.textContent = "";
// Stop the alarm sound after 15 seconds
setTimeout(() => {
alarmSound.pause();
alarmSound.currentTime = 0;
}, 15000);
}, timeToAlarm);
} else {
alarmStatus.textContent = "Please set a future time";
}
} else {
alarmStatus.textContent = "Please set a valid time";
}
});