|
1 |
| -function setAlarm() {} |
| 1 | +// Given an input area of Time counter and 2 buttons: 'Set Alarm' and 'Stop Alarm'. |
| 2 | +// When user clicks the `Set Alarm` button the counter at the |
| 3 | +// top of the screen should change to the entered number in the `input` field. |
| 4 | +// For example, |
| 5 | +// if the `input` field says `10` then the title should say `Time Remaining: 00:10`. |
| 6 | +// Every one second the title should count down by one. |
| 7 | + |
| 8 | +// When the `Time Remaining` reaches `00:00` the alarm should play a sound. You can |
| 9 | +// make the sound happen by using `playAlarm()`. |
| 10 | +// You can stop the alarm sound by pressing the `Stop Alarm` button. |
| 11 | + |
| 12 | +const alarmInputArea = document.querySelector("#alarmSet"); |
| 13 | +console.log(alarmInputArea, "<---- InputArea"); |
| 14 | + |
| 15 | +const setAlarmButton = document.querySelector("#set"); |
| 16 | +console.log(setAlarmButton, "<---- Alarm Button"); |
| 17 | + |
| 18 | +const setStopButton = document.querySelector("#stop"); |
| 19 | +console.log(setStopButton, "< ---- Stop Button"); |
| 20 | + |
| 21 | +const timeRemainInfo = document.querySelector("#timeRemaining"); |
| 22 | +console.log(timeRemainInfo, "<------Remain Time"); |
| 23 | + |
| 24 | +const backgroundColor = document.querySelector("html"); |
| 25 | +console.log(backgroundColor, "<------Background Color"); |
| 26 | + |
| 27 | +let intervalId; |
| 28 | + |
| 29 | +function setAlarm() { |
| 30 | + let inputInfo = Number(alarmInputArea.value); |
| 31 | + if (inputInfo < 1) { |
| 32 | + console.log("You need to input a value"); |
| 33 | + window.alert("You need to input a value!"); |
| 34 | + return; |
| 35 | + } |
| 36 | + alarmInputArea.value = ""; |
| 37 | + const mins = String(Math.floor(inputInfo / 60)).padStart(2, "0"); |
| 38 | + const seconds = String(inputInfo % 60).padStart(2, "0"); |
| 39 | + timeRemainInfo.textContent = `Time Remaining: ${mins}:${seconds}`; |
| 40 | + |
| 41 | + intervalId = setInterval(function () { |
| 42 | + if (inputInfo <= 0) { |
| 43 | + clearInterval(intervalId); |
| 44 | + playAlarm(); |
| 45 | + changeBackgroundColor(); |
| 46 | + return; |
| 47 | + } else { |
| 48 | + inputInfo--; |
| 49 | + const mins = String(Math.floor(inputInfo / 60)).padStart(2, "0"); |
| 50 | + const seconds = String(inputInfo % 60).padStart(2, "0"); |
| 51 | + timeRemainInfo.textContent = `Time Remaining: ${mins}:${seconds}`; |
| 52 | + } |
| 53 | + }, 1000); |
| 54 | +} |
| 55 | + |
| 56 | +//setAlarmButton.addEventListener("click", function setAlarm() { |
| 57 | +//console.log("click event is firing..."); |
| 58 | +//}); |
| 59 | + |
| 60 | +//setStopButton.addEventListener("click", function stopAlarm() { |
| 61 | +//console.log("click event is firing..."); |
| 62 | +//clearInterval(intervalId); |
| 63 | +//pauseAlarm(); |
| 64 | +//}); |
| 65 | + |
| 66 | +//alarm sound stop}) |
| 67 | + |
| 68 | +function changeBackgroundColor() { |
| 69 | + backgroundColor.style.backgroundColor = "lightblue"; |
| 70 | +} |
2 | 71 |
|
3 | 72 | // DO NOT EDIT BELOW HERE
|
4 | 73 |
|
|
0 commit comments