Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion Sprint-3/alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
function setAlarm() {}
const oneSecondInMilliseconds = 1000;
const lengthOfAlarmSound = 20000;

let timeLeft;
let intervalID;
let timeoutID;
let display;

function setAlarm() {
stopAlarm();

// set up time
timeLeft = document.getElementById("alarmSet").value;
display = document.getElementById("timeRemaining");

// validate result
const result = parseInt(timeLeft);
if (isNaN(result)) {
return;
}

display.textContent = formatTime(timeLeft);

if (!intervalID) {
intervalID = setInterval(updateTimer, 1000);
}

console.log(`Alarm set for ${timeLeft} seconds`);
}

function updateTimer() {
timeLeft = timeLeft - 1;

display.textContent = formatTime(timeLeft);

if (timeLeft <= 0) {
alarmFinished();
cleanUp();
}
}

// give a time in seconds
function formatTime(seconds) {
minutes = Math.floor(seconds / 60);
seconds = seconds % 60;

const seconds_padded = String(seconds).padStart(2, "0");
const minutes_paddded = String(minutes).padStart(2, "0");

const formatted_seconds = `Time Remaining: ${minutes_paddded}:${seconds_padded}`;
return formatted_seconds;
}

function stopAlarm() {
pauseAlarm();
cleanUp();
}

function cleanUp() {
clearInterval(intervalID);
clearTimeout(timeoutID);
timeoutID = null;
intervalID = null;
}

function alarmFinished() {
audio.loop = true;
playAlarm();
console.log("Alarm finished");
}

// DO NOT EDIT BELOW HERE

Expand Down
4 changes: 2 additions & 2 deletions Sprint-3/alarmclock/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Title here</title>
<title>Alarm clock app</title>
</head>
<body>
<div class="centre">
Expand Down
Loading