-
-
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 1 commit
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,52 @@ | ||
| function setAlarm() {} | ||
| // Stores the interval ID so we can clear it later | ||
| let interval = null; | ||
|
|
||
| function setAlarm() { | ||
| // Clear any previously running countdown | ||
| clearInterval(interval); | ||
|
|
||
| // Reset background to default | ||
| document.body.style.backgroundColor = ""; | ||
|
|
||
| // 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.
|
||
|
|
||
| // --- Format and display the initial time --- | ||
| // Math.floor(119 / 60) = 1 --> minutes | ||
| // 119 % 60 = 59 --> leftover seconds | ||
| // padStart(2, "0") ensures single digits become "01", "09", etc. | ||
| 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; | ||
|
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. Instead of keeping several copies of the code for displaying time (lines 18-21, lines 44-47, lines 32-33), Advantages:
|
||
|
|
||
| // --- Start the countdown --- | ||
| // setInterval calls the callback every 1000 ms (1 second) and returns an ID | ||
| interval = setInterval(function () { | ||
| totalSeconds--; // subtract 1 second | ||
|
|
||
| if (totalSeconds <= 0) { | ||
| // Stop the interval when time runs out | ||
| clearInterval(interval); | ||
|
|
||
| document.getElementById("timeRemaining").innerText = | ||
| "Time Remaining: 00:00"; | ||
|
|
||
| // Change background color 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; | ||
| } | ||
|
|
||
| // Recalculate mm:ss for the new totalSeconds and update the display | ||
| const m = String(Math.floor(totalSeconds / 60)).padStart(2, "0"); | ||
| const s = String(totalSeconds % 60).padStart(2, "0"); | ||
| document.getElementById("timeRemaining").innerText = | ||
| "Time Remaining: " + m + ":" + s; | ||
| }, 1000); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.