-
-
Notifications
You must be signed in to change notification settings - Fork 279
Sheffield | ITP-Jan-26 | Hayriye Saricicek | Sprint 3 | alarm clock #1113
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
Open
mshayriyesaricicek
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
mshayriyesaricicek:Sprint-3-Alarm-Clock-App-Clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2c81bf
function for alarm clock
mshayriyesaricicek 71f2fa0
updated variable names and amended code and added comments
mshayriyesaricicek e7e990d
saved file
mshayriyesaricicek 5eecda5
amended so no delay after 0 and have to press stop before set new alarm
mshayriyesaricicek 6cd5bcf
addedd functions and made amendments
mshayriyesaricicek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,118 @@ | ||
| function setAlarm() {} | ||
| // Only define Audio if it doesn't exist (Node environment) | ||
| if (typeof Audio === "undefined") { | ||
| // In a Node define a mock Audio class to prevent errors when calling play() and pause() | ||
| global.Audio = class { | ||
| // Mock Audio class | ||
| constructor(src) { | ||
| // Takes a source for the audio file, but we won't load audio in this mock | ||
| this.src = src; // stores the source (unused in the mock) | ||
| } | ||
| play() {} // no-op in this mock | ||
| pause() {} // no-op in this mock | ||
| }; | ||
| } | ||
|
|
||
| let countdownIntervalId; //stores timer ID so we can clear it when needed | ||
| let alarmRunning = false; // tracks if a countdown is currently active | ||
|
|
||
| const formatTime = (seconds) => { | ||
| // takes a number of seconds and formats it into a string in the format "MM:SS" | ||
| const minutes = Math.floor(seconds / 60); // calculates whole minutes | ||
| const remainingSeconds = seconds % 60; // calculates remaining seconds | ||
| return `${String(minutes).padStart(2, "0")}:${String(remainingSeconds).padStart(2, "0")}`; // formats as MM:SS | ||
| }; | ||
|
|
||
| function setAlarm() { | ||
| // called "Set" button is clicked | ||
| const alarmInput = document.getElementById("alarmSet"); // gets input for seconds | ||
| const timerDisplay = document.getElementById("timeRemaining"); // gets element showing remaining time | ||
|
|
||
| // Prevent starting a new alarm if one is already running | ||
| if (alarmRunning) { | ||
| alert("Please press Stop before setting a new alarm."); // ensues user cannot set another alarm without stopping the current one first | ||
| return; | ||
| } | ||
| const seconds = parseInt(alarmInput.value, 10); // converts input value to integer | ||
|
|
||
| if (isNaN(seconds) || seconds < 0) return; // if ignore invalid or negative input | ||
|
|
||
| let remainingSeconds = seconds; // initializes countdown | ||
|
|
||
| timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // shows initial time | ||
|
|
||
| if (countdownIntervalId) { | ||
| clearInterval(countdownIntervalId); // stops previous countdown to prevent multiple timers from running simultaneously | ||
| countdownIntervalId = null; // resets timer ID | ||
| } | ||
|
|
||
| if (remainingSeconds === 0) { | ||
| // if the input is zero, play the alarm immediately without starting a countdown | ||
| playAlarm(); | ||
| return; | ||
| } | ||
|
|
||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| alarmRunning = true; // mark that alarm is active | ||
| countdownIntervalId = setInterval(() => { | ||
| remainingSeconds--; // decrements remaining time by 1 second | ||
|
|
||
| if (remainingSeconds <= 0) { | ||
| // when time runs out, clear the timer and play the alarm sound | ||
| clearInterval(countdownIntervalId); // stops the countdown timer | ||
| countdownIntervalId = null; // resets timer ID | ||
| alarmRunning = false; // mark that alarm is no longer active | ||
| timerDisplay.textContent = "Time Remaining: 00:00"; // shows zero time | ||
| playAlarm(); // plays alarm sound | ||
| return; // prevents further execution | ||
| } | ||
| timerDisplay.textContent = `Time Remaining: ${formatTime(remainingSeconds)}`; // updates countdown display | ||
| }, 1000); // repeats every second | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| window.addEventListener("load", () => { | ||
| // Add a listener to the Stop button that also clears the timer | ||
| const stopBtn = document.getElementById("stop"); | ||
|
|
||
| stopBtn.addEventListener("click", () => { | ||
| // stops countdown amd stops audio | ||
| if (countdownIntervalId) { | ||
| clearInterval(countdownIntervalId); | ||
| countdownIntervalId = null; | ||
| } | ||
| alarmRunning = false; // mark that alarm is no longer active | ||
|
|
||
| document.getElementById("timeRemaining").textContent = | ||
| "Time Remaining: 00:00"; // resets display when stopped | ||
|
|
||
| document.getElementById("alarmSet").value = ""; // clears input field when stopped | ||
| pauseAlarm(); // stop audio if it's playing | ||
| }); | ||
|
Comment on lines
+101
to
+114
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. Why not just call |
||
| }); | ||
|
|
||
| // DO NOT EDIT BELOW HERE | ||
|
|
||
| var audio = new Audio("alarmsound.mp3"); | ||
|
|
||
| function setup() { | ||
| // This function sets up event listeners for the "Set" and "Stop" buttons when the page loads | ||
| document.getElementById("set").addEventListener("click", () => { | ||
| setAlarm(); | ||
| // adds a click event listener to the "Set" button that calls the setAlarm function when clicked | ||
| setAlarm(); // calls the setAlarm function to start the timer when the "Set" button is clicked | ||
| }); | ||
|
|
||
| document.getElementById("stop").addEventListener("click", () => { | ||
| pauseAlarm(); | ||
| // adds a click event listener to the "Stop" button that calls the pauseAlarm function when clicked | ||
| pauseAlarm(); // calls the pauseAlarm function to stop the alarm sound when the "Stop" button is clicked | ||
| }); | ||
| } | ||
|
|
||
| function playAlarm() { | ||
| audio.play(); | ||
| // This function plays the alarm sound when called | ||
| audio.play(); // calls the play method on the audio object to start playing the alarm sound | ||
| } | ||
|
|
||
| function pauseAlarm() { | ||
| audio.pause(); | ||
| // This function pauses the alarm sound when called | ||
| audio.pause(); // calls the pause method on the audio object to stop playing the alarm sound | ||
| } | ||
|
|
||
| window.onload = setup; | ||
| window.onload = setup; // sets the setup function to run when the window finishes loading, ensuring that event listeners are set up properly | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.