Skip to content

Commit 5eecda5

Browse files
amended so no delay after 0 and have to press stop before set new alarm
1 parent e7e990d commit 5eecda5

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if (typeof Audio === "undefined") {
1313
}
1414

1515
let countdownIntervalId; //stores timer ID so we can clear it when needed
16+
let alarmRunning = false; // tracks if a countdown is currently active
1617

1718
const formatTime = (seconds) => {
1819
// takes a number of seconds and formats it into a string in the format "MM:SS"
@@ -26,22 +27,39 @@ function setAlarm() {
2627
const alarmInput = document.getElementById("alarmSet"); // gets input for seconds
2728
const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time
2829

30+
// Prevent starting a new alarm if one is already running
31+
if (alarmRunning) {
32+
alert("Please press Stop before setting a new alarm."); // ensues user cannot set another alarm without stopping the current one first
33+
return;
34+
}
2935
const seconds = parseInt(alarmInput.value, 10); // converts input value to integer
36+
3037
if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input
3138

3239
let remainingSeconds = seconds; // initializes countdown
3340

3441
timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time
3542

36-
if (countdownIntervalId) clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously
43+
if (countdownIntervalId) {
44+
clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously
45+
countdownIntervalId = null; // resets timer ID
46+
}
47+
48+
if (remainingSeconds === 0) {
49+
// if the input is zero, play the alarm immediately without starting a countdown
50+
playAlarm();
51+
return;
52+
}
3753

54+
alarmRunning = true; // mark that alarm is active
3855
countdownIntervalId = setInterval(() => {
39-
// timer runs every second
4056
remainingSeconds--; // decrements remaining time by 1 second
4157

4258
if (remainingSeconds <= 0) {
4359
// when time runs out, clear the timer and play the alarm sound
44-
clearInterval(countdownIntervalId); // stops the countdown
60+
clearInterval(countdownIntervalId); // stops the countdown timer
61+
countdownIntervalId = null; // resets timer ID
62+
alarmRunning = false; // mark that alarm is no longer active
4563
timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time
4664
playAlarm(); // plays alarm sound
4765
return; // prevents further execution
@@ -50,6 +68,26 @@ function setAlarm() {
5068
}, 1000); // repeats every second
5169
}
5270

71+
window.addEventListener("load", () => {
72+
// Add a listener to the Stop button that also clears the timer
73+
const stopBtn = document.getElementById("stop");
74+
75+
stopBtn.addEventListener("click", () => {
76+
// stops countdown amd stops audio
77+
if (countdownIntervalId) {
78+
clearInterval(countdownIntervalId);
79+
countdownIntervalId = null;
80+
}
81+
alarmRunning = false; // mark that alarm is no longer active
82+
83+
document.getElementById("timeRemaining").textContent =
84+
"Time Remaining: 00:00"; // resets display when stopped
85+
86+
document.getElementById("alarmSet").value = ""; // clears input field when stopped
87+
pauseAlarm(); // stop audio if it's playing
88+
});
89+
});
90+
5391
// DO NOT EDIT BELOW HERE
5492

5593
var audio = new Audio("alarmsound.mp3");

Sprint-3/alarmclock/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="centre">
1111
<h1 id="timeRemaining">Time Remaining: 00:00</h1>
1212
<label for="alarmSet">Set time to:</label>
13-
<input id="alarmSet" type="number" />
13+
<input id="alarmSet" type="number" min="0" />
1414

1515
<button id="set" type="button">Set Alarm</button>
1616
<button id="stop" type="button">Stop Alarm</button>

0 commit comments

Comments
 (0)