-
-
Notifications
You must be signed in to change notification settings - Fork 283
Sheffield | 26-ITP-Jan | Mahmoud Shaabo | Sprint 3 | Alarm Clock App #1155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,79 @@ | ||
| function setAlarm() {} | ||
| // Stores the interval ID so we can clear it later | ||
| let interval = null; | ||
|
|
||
| // Helper function: format seconds as mm:ss and update the display | ||
| // This keeps the time-display logic in ONE place (CJ feedback: avoid repetition) | ||
| function updateTimeDisplay(totalSeconds) { | ||
| const mins = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); | ||
| const secs = String(totalSeconds % 60).padStart(2, "0"); | ||
| document.getElementById("timeRemaining").innerText = | ||
| "Time Remaining: " + mins + ":" + secs; | ||
| } | ||
|
|
||
| function setAlarm() { | ||
| // Clear any previously running countdown | ||
| clearInterval(interval); | ||
|
|
||
| // Reset background to default | ||
| document.body.style.backgroundColor = ""; | ||
|
|
||
| // Stop any currently playing alarm sound before starting a new countdown | ||
| // (the user may not click "Stop" first before setting a new alarm) | ||
| pauseAlarm(); | ||
|
Comment on lines
+20
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the resetting logic is becoming more complicated, you can consider placing them in a function. |
||
|
|
||
| // Read the input value and convert it to a plain integer (total seconds) | ||
| let totalSeconds = parseInt(document.getElementById("alarmSet").value); | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| // Validate input: reject NaN, negative numbers, or empty field | ||
| if (isNaN(totalSeconds) || totalSeconds < 0) { | ||
| document.getElementById("timeRemaining").innerText = | ||
| "Please enter a valid number of seconds (0 or above)."; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to introduce a separate element to show feedback message, and leave "#timeRemaining" for only displaying time. |
||
| return; | ||
| } | ||
|
|
||
| // If input is 0, trigger the alarm immediately instead of waiting 1 second | ||
| if (totalSeconds === 0) { | ||
| updateTimeDisplay(0); | ||
| document.body.style.backgroundColor = "red"; | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| // Display the initial time using the helper function | ||
| updateTimeDisplay(totalSeconds); | ||
|
|
||
| // Start the countdown: setInterval calls the callback every 1000 ms (1 second) | ||
| interval = setInterval(function () { | ||
| totalSeconds--; // subtract 1 second | ||
|
|
||
| if (totalSeconds <= 0) { | ||
| // Stop the interval when time runs out | ||
| clearInterval(interval); | ||
|
|
||
| updateTimeDisplay(0); | ||
|
|
||
| // Change background colour to signal the alarm | ||
| document.body.style.backgroundColor = "red"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To better separate presentation logic from application logic, you can consider defining a CSS class, and use |
||
|
|
||
| // Trigger the alarm sound (defined below the DO NOT EDIT line) | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
| // Update the display with the new remaining time | ||
| updateTimeDisplay(totalSeconds); | ||
| }, 1000); | ||
| } | ||
|
|
||
| // Make the Stop button also stop the countdown (not just the sound) | ||
| // Uses DOMContentLoaded so the button element exists before we attach the listener | ||
| // This adds a SECOND listener without modifying the original code below | ||
| document.addEventListener("DOMContentLoaded", function () { | ||
| document.getElementById("stop").addEventListener("click", function () { | ||
| clearInterval(interval); | ||
| document.body.style.backgroundColor = ""; | ||
| }); | ||
| }); | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.