-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
98 lines (86 loc) · 2.88 KB
/
script.js
File metadata and controls
98 lines (86 loc) · 2.88 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
let timer;
let isRunning = false;
let timeLeft = 1500; // Default: 25 minutes
const minutesDisplay = document.getElementById("minutes");
const secondsDisplay = document.getElementById("seconds");
const startButton = document.getElementById("start");
const pauseButton = document.getElementById("pause");
const resetButton = document.getElementById("reset");
const alarmSound = document.getElementById("alarm");
const progressBar = document.getElementById("progress-bar");
const darkModeButton = document.getElementById("toggle-dark-mode");
// Update Timer Display
function updateDisplay() {
let minutes = Math.floor(timeLeft / 60);
let seconds = timeLeft % 60;
minutesDisplay.textContent = minutes < 10 ? `0${minutes}` : minutes;
secondsDisplay.textContent = seconds < 10 ? `0${seconds}` : seconds;
}
// Update Progress Bar
function updateProgress() {
let progress = ((1500 - timeLeft) / 1500) * 100;
progressBar.style.width = `${progress}%`;
}
// Start Timer
function startTimer() {
if (!isRunning) {
isRunning = true;
timer = setInterval(() => {
if (timeLeft > 0) {
timeLeft--;
updateDisplay();
updateProgress();
} else {
clearInterval(timer);
isRunning = false;
alarmSound.play();
document.body.style.backgroundColor = "#ff6961"; // Flash red
setTimeout(() => document.body.style.backgroundColor = "", 2000);
alert("Time's up!");
}
}, 1000);
}
}
// Pause Timer
function pauseTimer() {
clearInterval(timer);
isRunning = false;
}
// Reset Timer
function resetTimer() {
clearInterval(timer);
isRunning = false;
timeLeft = 1500;
updateDisplay();
updateProgress();
}
// Set Custom Time
document.getElementById("set-time").addEventListener("click", () => {
let customTime = parseInt(document.getElementById("custom-time").value);
if (!isNaN(customTime) && customTime > 0) {
timeLeft = customTime * 60;
updateDisplay();
updateProgress();
}
});
// Toggle Dark Mode
darkModeButton.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
if (document.body.classList.contains("dark-mode")) {
darkModeButton.textContent = "☀️ Light Mode";
localStorage.setItem("darkMode", "enabled");
} else {
darkModeButton.textContent = "🌙 Dark Mode";
localStorage.setItem("darkMode", "disabled");
}
});
// Remember Dark Mode Setting
if (localStorage.getItem("darkMode") === "enabled") {
document.body.classList.add("dark-mode");
darkModeButton.textContent = "☀️ Light Mode";
}
// Event Listeners
startButton.addEventListener("click", startTimer);
pauseButton.addEventListener("click", pauseTimer);
resetButton.addEventListener("click", resetTimer);
updateDisplay();