Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.
Prev Previous commit
Next Next commit
alarmclock completed
  • Loading branch information
bireworld committed Aug 1, 2020
commit f3e4fcb36e9ab2fc9fb6c42f10eb5312f8da53ef
31 changes: 30 additions & 1 deletion Week-3/Homework/mandatory/1-alarmclock/alarmclock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
function setAlarm() {}
let alarmTimer;
function setAlarm() {
var alarmSet = document.getElementById("alarmSet").value;
var timeRemaining = document.getElementById("timeRemaining");
updateTime(alarmSet, timeRemaining);

// clear previous timer
clearInterval(alarmTimer);

alarmTimer = setInterval(() => {
updateTime(alarmSet, timeRemaining);
}, 1000);

function updateTime(sec, node) {
if (sec < 1) {
clearInterval(alarmTimer);
playAlarm();
}
const time = formatTime(sec);
node.textContent = `Time Remaining: ${time}`;
alarmSet--;
}

function formatTime(seconds) {
const s = alarmSet % 60;
const m = Math.floor(alarmSet / 60);

return ` ${m < 10 ? `0${m}` : m}:${s < 10 ? `0${s}` : s}`;
}
}

// DO NOT EDIT BELOW HERE

Expand Down