Skip to content

Commit 7c69bbe

Browse files
author
Innocent_Niwatwine
committed
Functions added
1 parent 0b93f29 commit 7c69bbe

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

Sprint-3/alarmclock/alarmclock.js

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
1-
let countdownInterval;
2-
let timeRemaining = 0;
1+
let countdown;
2+
let ispaused = false;
3+
let timeRemaining;
34

45
function setAlarm(){
5-
pauseAlarm();
6-
audio.currentTime = 0
7-
clearInterval(countdownInterval);
86
const inputField = document.getElementById("alarmSet");
9-
timeRemaining = parseInt(inputField.value, 10);
7+
timeRemaining = parseInt(inputField.value);
108

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();
1516

16-
updateTimerDisplay();
17-
countdownInterval = setInterval(() => {
18-
timeRemaining--;
17+
countdown = setInterval(() => {
18+
if (!isPaused){
19+
timeRemaining = timeRemaining - 1;
20+
updateTitle();
1921

2022
if (timeRemaining <= 0) {
21-
clearInterval(countdownInterval);
22-
playAlarm();
23+
clearInterval(countdown);
24+
playAlarm();
2325
}
24-
updateTimerDisplay();
25-
26+
}
2627
}, 1000);
2728
}
2829

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";
3433
}
3534

36-
37-
38-
3935
// DO NOT EDIT BELOW HERE
4036

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+
}
4150

51+
function playAlarm() {
52+
audio.play();
53+
}
54+
function pauseAlarm() {
55+
audio.pause();
56+
}
57+
window.onload = setup;
4258

4359

4460

Sprint-3/alarmclock/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ <h1 id="timeRemaining">Time Remaining: 00:00</h1>
1313
<input id="alarmSet" type="number" />
1414

1515
<button id="set" type="button">Set Alarm</button>
16+
<button id="pause" type="button">Pause Timer</button>
1617
<button id="stop" type="button">Stop Alarm</button>
1718
</div>
1819
<script src="alarmclock.js"></script>

0 commit comments

Comments
 (0)