-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
99 lines (86 loc) · 2.61 KB
/
Copy pathscript.js
File metadata and controls
99 lines (86 loc) · 2.61 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
99
let timer;
let time = 0;
let isRunning = false;
let mode = "stopwatch";
let savedTimers = JSON.parse(localStorage.getItem("savedTimers")) || [];
const display = document.getElementById("timerDisplay");
const startBtn = document.getElementById("startBtn");
const stopBtn = document.getElementById("stopBtn");
const resetBtn = document.getElementById("resetBtn");
const stopwatchBtn = document.getElementById("stopwatchBtn");
const countdownBtn = document.getElementById("countdownBtn");
const saveBtn = document.getElementById("saveBtn");
const labelInput = document.getElementById("labelInput");
const showSavedBtn = document.getElementById("showSavedBtn");
const savedScreen = document.getElementById("savedTimersScreen");
const closeSavedBtn = document.getElementById("closeSavedBtn");
const savedList = document.getElementById("savedTimersList");
const beep = document.getElementById("beepSound");
function updateDisplay() {
const hours = String(Math.floor(time / 3600)).padStart(2, "0");
const minutes = String(Math.floor((time % 3600) / 60)).padStart(2, "0");
const seconds = String(time % 60).padStart(2, "0");
display.textContent = `${hours}:${minutes}:${seconds}`;
}
function tick() {
if (mode === "stopwatch") {
time++;
} else if (mode === "countdown") {
if (time > 0) {
time--;
if (time === 0) beep.play();
}
}
updateDisplay();
}
startBtn.onclick = () => {
if (!isRunning) {
isRunning = true;
timer = setInterval(tick, 1000);
}
};
stopBtn.onclick = () => {
isRunning = false;
clearInterval(timer);
};
resetBtn.onclick = () => {
isRunning = false;
clearInterval(timer);
time = mode === "countdown" ? prompt("Set countdown seconds:") || 0 : 0;
updateDisplay();
};
stopwatchBtn.onclick = () => {
mode = "stopwatch";
time = 0;
updateDisplay();
};
countdownBtn.onclick = () => {
mode = "countdown";
time = parseInt(prompt("Enter countdown in seconds:"), 10) || 0;
updateDisplay();
};
saveBtn.onclick = () => {
if (savedTimers.length >= 6) {
alert("Max 6 timers saved.");
return;
}
const label = labelInput.value.trim();
if (label) {
savedTimers.push({ label, type: mode, time });
localStorage.setItem("savedTimers", JSON.stringify(savedTimers));
labelInput.value = "";
}
};
showSavedBtn.onclick = () => {
savedList.innerHTML = "";
savedTimers.forEach((t, i) => {
const li = document.createElement("li");
li.textContent = `${t.label} (${t.type}) - ${t.time}s`;
savedList.appendChild(li);
});
savedScreen.classList.remove("hidden");
};
closeSavedBtn.onclick = () => {
savedScreen.classList.add("hidden");
};
updateDisplay();