-
-
Notifications
You must be signed in to change notification settings - Fork 195
London | 25-ITP-Sept | Samuel Tarawally | Sprint 3 | Alarm Clock #929
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 all commits
c3824f3
3a0ada8
c964595
895d8c6
1aff7c8
f08fbfc
7342082
8f1a667
fbc7a56
548ef16
426f1fb
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,92 @@ | ||
| function setAlarm() {} | ||
| const titleElement = document.getElementById("timeRemaining"); | ||
|
|
||
| const ONE_SECOND_IN_MILLISECONDS = 1000; | ||
|
|
||
| let timeRemainingInSeconds = 0; | ||
| let alarmTimerIdentifier = null; | ||
|
|
||
| /** | ||
| * Formats seconds into "MM:SS". | ||
| */ | ||
| function formatTime(totalSeconds) { | ||
| const minutes = Math.floor(totalSeconds / 60); | ||
| const seconds = totalSeconds % 60; | ||
|
|
||
| const formattedMinutes = String(minutes).padStart(2, '0'); | ||
| const formattedSeconds = String(seconds).padStart(2, '0'); | ||
|
|
||
| return `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; | ||
| } | ||
|
|
||
| /** | ||
| * Displays the remaining time. | ||
| */ | ||
| function displayTime() { | ||
| titleElement.innerText = formatTime(timeRemainingInSeconds); | ||
| } | ||
|
|
||
| /** | ||
| * Updates the display and checks if the alarm should sound. | ||
| */ | ||
| function updateTime() { | ||
| timeRemainingInSeconds = timeRemainingInSeconds - 1; | ||
| displayTime(); | ||
|
|
||
| if (timeRemainingInSeconds === 0) { | ||
| playAlarm(); | ||
| document.body.classList.add("flash"); | ||
| clearInterval(alarmTimerIdentifier); | ||
| alarmTimerIdentifier = null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initialises the alarm countdown. | ||
| */ | ||
| function setAlarm() { | ||
| if (alarmTimerIdentifier) { | ||
| clearInterval(alarmTimerIdentifier); | ||
| document.body.classList.remove("flash"); | ||
| } | ||
|
|
||
| const timeInput = document.getElementById("alarmSet"); | ||
| const startingTime = parseInt(timeInput.value, 10); | ||
|
|
||
| if (isNaN(startingTime)) { | ||
| return; | ||
| } | ||
|
|
||
| if (startingTime < 0) { | ||
| return; | ||
| } | ||
|
|
||
| timeRemainingInSeconds = startingTime; | ||
|
|
||
| // Initialise the audio context to enable autoplay. | ||
| audio.play(); | ||
| audio.pause(); | ||
|
|
||
| displayTime(); | ||
|
|
||
| // Handle zero-second edge case | ||
| if (timeRemainingInSeconds === 0) { | ||
| playAlarm(); | ||
| document.body.classList.add("flash"); | ||
| return; | ||
| } | ||
|
|
||
| alarmTimerIdentifier = setInterval(() => { | ||
| updateTime(); | ||
| }, ONE_SECOND_IN_MILLISECONDS); | ||
| } | ||
|
Comment on lines
+78
to
+81
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. The alarm will sound one second afterward when the input is either 0 or 1 second -- it is a bit inconsistent. Can you improve the consistency?
Author
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. I'll add a conditional statement to handle this case. |
||
|
|
||
| // Add a listener to stop the flashing effect without modifying the protected pauseAlarm function below. | ||
| const stopButton = document.getElementById("stop"); | ||
| if (stopButton) { | ||
| stopButton.addEventListener("click", () => { | ||
| document.body.classList.remove("flash"); | ||
| }); | ||
| } | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,97 @@ | ||
| .centre { | ||
| position: fixed; | ||
| top: 50%; | ||
| left: 50%; | ||
| -webkit-transform: translate(-50%, -50%); | ||
| transform: translate(-50%, -50%); | ||
| body { | ||
| background-color: #f0f0f0; | ||
| font-family: Arial, Helvetica, sans-serif; | ||
| color: #333; | ||
| display: flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
| min-height: 100vh; | ||
| margin: 0; | ||
| } | ||
|
|
||
| #alarmSet { | ||
| margin: 20px; | ||
| .flash { | ||
| animation: flash-animation 1s infinite; | ||
| } | ||
|
|
||
| /* Flashing background colour animation */ | ||
| @keyframes flash-animation { | ||
| 0% { | ||
| background-color: #ffcccc; | ||
| } | ||
| 50% { | ||
| background-color: #ff0000; | ||
| } | ||
| 100% { | ||
| background-color: #ffcccc; | ||
| } | ||
| } | ||
|
|
||
| .container { | ||
| background-color: #fff; | ||
| border: 1px solid #ccc; | ||
| border-radius: 8px; | ||
| padding: 30px; | ||
| box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); | ||
| text-align: center; | ||
| max-width: 500px; | ||
| width: 90%; | ||
| } | ||
|
|
||
| h1 { | ||
| font-size: 2.5rem; | ||
| margin-bottom: 20px; | ||
| color: #333; | ||
| } | ||
|
|
||
| .input-group { | ||
| margin-bottom: 20px; | ||
| } | ||
|
|
||
| label { | ||
| display: block; | ||
| margin-bottom: 8px; | ||
| font-size: 1.1rem; | ||
| } | ||
|
|
||
| input { | ||
| padding: 10px; | ||
| font-size: 1.2rem; | ||
| border: 1px solid #ccc; | ||
| border-radius: 4px; | ||
| width: 100px; | ||
| text-align: center; | ||
| } | ||
|
|
||
| input:focus { | ||
| outline: none; | ||
| border-color: #007bff; | ||
| } | ||
|
|
||
| .controls { | ||
| display: flex; | ||
| gap: 10px; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| button { | ||
| background-color: #007bff; | ||
| color: white; | ||
| border: none; | ||
| padding: 10px 20px; | ||
| font-size: 1rem; | ||
| cursor: pointer; | ||
| border-radius: 4px; | ||
| transition: background-color 0.2s; | ||
| } | ||
|
|
||
| button:hover { | ||
| background-color: #0056b3; | ||
| } | ||
|
|
||
| button#stop { | ||
| background-color: #dc3545; | ||
| } | ||
|
|
||
| button#stop:hover { | ||
| background-color: #a71d2a; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once set,
alarmTimerIdentifieris never reset. So this condition will only befalsethe first timesetAlarm()is called. Well, do we need to check or reset this variable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable needs to be reset otherwise the condition will always be true after the first alarm.