|
1 | | -let countdownInterval; |
2 | | -let timeRemaining = 0; |
| 1 | +let countdown; |
| 2 | +let ispaused = false; |
| 3 | +let timeRemaining; |
3 | 4 |
|
4 | 5 | function setAlarm(){ |
5 | | - pauseAlarm(); |
6 | | - audio.currentTime = 0 |
7 | | - clearInterval(countdownInterval); |
8 | 6 | const inputField = document.getElementById("alarmSet"); |
9 | | - timeRemaining = parseInt(inputField.value, 10); |
| 7 | + timeRemaining = parseInt(inputField.value); |
10 | 8 |
|
11 | | - if (isNaN(timeRemaining) || timeRemaining <= 0) { |
12 | | - alert("Please enter a valid number greater than 0."); |
13 | | - return; |
14 | | - } |
| 9 | + const updateTitle = () => { |
| 10 | + const minutes = String(Math.floor(timeRemaining/60)).padStart(2, '0'); // minutes |
| 11 | + const seconds = String(timeRemaining % 60).padStart(2, '0'); // seconds |
| 12 | + document.getElementById("timeRemaining").innerText = `Time Remaining: ${minutes}:${seconds}`; |
| 13 | + }; |
| 14 | + |
| 15 | + updateTitle(); |
15 | 16 |
|
16 | | - updateTimerDisplay(); |
17 | | - countdownInterval = setInterval(() => { |
18 | | - timeRemaining--; |
| 17 | + countdown = setInterval(() => { |
| 18 | + if (!isPaused){ |
| 19 | + timeRemaining = timeRemaining - 1; |
| 20 | + updateTitle(); |
19 | 21 |
|
20 | 22 | if (timeRemaining <= 0) { |
21 | | - clearInterval(countdownInterval); |
22 | | - playAlarm(); |
| 23 | + clearInterval(countdown); |
| 24 | + playAlarm(); |
23 | 25 | } |
24 | | - updateTimerDisplay(); |
25 | | - |
| 26 | + } |
26 | 27 | }, 1000); |
27 | 28 | } |
28 | 29 |
|
29 | | -function updateTimerDisplay() { |
30 | | - const title = document.getElementById("timeRemaining"); |
31 | | - const minutes = String(Math.floor(timeRemaining / 60)).padStart(2, "0"); |
32 | | - const seconds = String(timeRemaining % 60).padStart(2, "0"); |
33 | | - title.textContent = `Time Remaining: ${minutes}:${seconds}`; |
| 30 | +function pauseTimer() { |
| 31 | +isPaused = !isPaused; |
| 32 | +document.getElementById("pause").innerText = isPaused ? "Resume Timer" : "Pause Timer"; |
34 | 33 | } |
35 | 34 |
|
36 | | - |
37 | | - |
38 | | - |
39 | 35 | // DO NOT EDIT BELOW HERE |
40 | 36 |
|
| 37 | +var audio = new Audio("alarmsound.mp3"); |
| 38 | +function setup() { |
| 39 | + document.getElementById("set").addEventListener("click", () => { |
| 40 | + setAlarm(); |
| 41 | + }); |
| 42 | + document.getElementById("stop").addEventListener("click", () => { |
| 43 | + pauseAlarm(); |
| 44 | + }); |
| 45 | + |
| 46 | + document.getElementById("pause").addEventListener("click", () => { |
| 47 | + pauseTimer(); |
| 48 | + }); |
| 49 | +} |
41 | 50 |
|
| 51 | +function playAlarm() { |
| 52 | + audio.play(); |
| 53 | +} |
| 54 | +function pauseAlarm() { |
| 55 | + audio.pause(); |
| 56 | +} |
| 57 | +window.onload = setup; |
42 | 58 |
|
43 | 59 |
|
44 | 60 |
|
0 commit comments