-
-
Notifications
You must be signed in to change notification settings - Fork 281
London | 26-ITP-JAN | Shuheda Begum | Sprint 3 | Alarm Clock #1008
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 5 commits
419218d
b2d54f4
ee3dc7d
3c60cb0
90e99cf
c6fcdd6
1724a28
f363ec0
7ead5c3
f691b29
3f63303
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,54 @@ | ||
| function setAlarm() {} | ||
| let timerInterval = null; | ||
| let remainingSeconds = 0; | ||
|
|
||
| function setAlarm() { | ||
| // Read the minutes value from the alarm input field | ||
| const minutesInput = document.getElementById("alarmSet"); | ||
| const minutes = parseInt(minutesInput.value, 10); | ||
| // Ignore invalid or non-positive input | ||
| if (isNaN(minutes) || minutes <= 0) { | ||
| return; | ||
| } | ||
| // Store the input as the remaining seconds to count down | ||
| remainingSeconds = minutes; | ||
| // Clear any existing timer before starting a new one | ||
| if (timerInterval !== null) { | ||
| clearInterval(timerInterval); | ||
| timerInterval = null; | ||
| } | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| // Immediately render the starting time on screen | ||
| updateTimeDisplay(); | ||
|
|
||
| // Tick every second and decrement remainingSeconds | ||
| timerInterval = setInterval(() => { | ||
| remainingSeconds = remainingSeconds - 1; | ||
|
|
||
| updateTimeDisplay(); | ||
|
|
||
| if (remainingSeconds <= 0) { | ||
| // Countdown finished — stop the timer and trigger the alarm | ||
| clearInterval(timerInterval); | ||
| timerInterval = null; | ||
| remainingSeconds = 0; | ||
| // Ensure the display shows exactly 00:00 | ||
| updateTimeDisplay(); | ||
|
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. If you modify Best practice is to avoid sharing variables through outer or global scope. Designing a function to take input through parameters can also improve the reusability of the function. Question: Can you elaborate the need to call
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. Yes, the first updateTimeDisplay() already shows 00:00 when the timer ends. |
||
| playAlarm(); | ||
| } | ||
| }, 1000); | ||
| } | ||
|
|
||
| // Converts remainingSeconds into MM:SS format and updates the display | ||
| function updateTimeDisplay() { | ||
| const minutes = Math.floor(remainingSeconds / 60); | ||
|
cjyuan marked this conversation as resolved.
|
||
| const seconds = remainingSeconds % 60; | ||
|
|
||
| const formattedMinutes = minutes.toString().padStart(2, "0"); | ||
| const formattedSeconds = seconds.toString().padStart(2, "0"); | ||
|
|
||
| const timeDisplay = document.getElementById("timeRemaining"); | ||
| timeDisplay.textContent = `Time Remaining: ${formattedMinutes}:${formattedSeconds}`; | ||
| } | ||
|
|
||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.