Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active June 29, 2023 01:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Javascript Alarm Clock

JAVASCRIPT ALARM CLOCK

https://code-boxx.com/simple-javascript-alarm-clock/

NOTES

Gist does not allow MP3 uploads... Do a "free alarm sound" search on your own, save it as wake-up-sound.mp3 in your project folder.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) SHARED */
#ctime *, #tpick * {
font-family: Impact, sans-serif;
box-sizing: border-box;
text-align: center;
}
#ctime, #tpick, .square { padding: 10px; }
#ctime, #tpick {
margin: 0 auto;
max-width: 350px;
display: flex;
flex-wrap: wrap;
}
.header {
font-weight: normal;
width: 100%;
margin: 10px 0;
}
.square, #tpick-h, #tpick-m, #tpick-s { width: 33%; }
.text, #tset, #treset { margin-top:10px; }
.digits, .square select {
font-size: 24px;
background: #fff;
color: #000;
border: 0;
border-radius: 5px;
width: 100%;
padding: 10px 0;
}
/* (B) CURRENT TIME */
#ctime { background: #000; }
#ctime .header { color: #c61d1d; }
#ctime .text { color: #ddd; }
/* (C) TIME PICKER */
#tpick { background: #f2f2f2; }
#tset, #treset {
width: 50%;
background: #3368b2;
color: #fff;
border: 0;
padding: 15px 0;
cursor: pointer;
font-size: 18px;
}
#tset:disabled, #treset:disabled {
background: #aaa;
color: #888;
}
<!DOCTYPE html>
<html>
<head>
<title>
Javascript Alarm Clock Demo
</title>
<link href="js-alarm-clock.css" rel="stylesheet">
<script defer src="js-alarm-clock.js"></script>
</head>
<body>
<!-- (A) CURRENT TIME -->
<div id="ctime">
<h1 class="header">THE CURRENT TIME</h1>
<div class="square">
<div class="digits" id="chr">00</div>
<div class="text">HR</div>
</div>
<div class="square">
<div class="digits" id="cmin">00</div>
<div class="text">MIN</div>
</div>
<div class="square">
<div class="digits" id="csec">00</div>
<div class="text">SEC</div>
</div>
</div>
<!-- (B) SET ALARM -->
<div id="tpick">
<h1 class="header">SET ALARM</h1>
<div id="tpick-h" class="square"></div>
<div id="tpick-m" class="square"></div>
<div id="tpick-s" class="square"></div>
<input type="button" value="Set" id="tset">
<input type="button" value="Reset" id="treset" disabled>
</div>
</body>
</html>
var ac = {
// (A) HTML ELEMENTS & PROPERTIES
// (A1) CLOCK HOUR, MIN, SEC
chr : null, cmin : null, csec : null,
// (A2) TIME PICKER HOUR, MIN, SEC, SET, RESET
thr : null, thm : null, ths : null,
tset : null, treset : null,
// (A3) ALARM TIME & SOUND
alarm : null, sound : null,
// (B) INITIALIZE ALARM CLOCK
init : () => {
// (B1) GET HTML CURRENT TIME - HOUR, MIN, SECONDS
ac.chr = document.getElementById("chr");
ac.cmin = document.getElementById("cmin");
ac.csec = document.getElementById("csec");
// (B2) CREATE HTML TIME PICKER - HR, MIN, SEC
ac.thr = ac.createSel(23);
ac.thm = ac.createSel(59);
ac.ths = ac.createSel(59);
document.getElementById("tpick-h").appendChild(ac.thr);
document.getElementById("tpick-m").appendChild(ac.thm);
document.getElementById("tpick-s").appendChild(ac.ths);
// (B3) CREATE HTML TIME PICKER - SET, RESET
ac.tset = document.getElementById("tset");
ac.treset = document.getElementById("treset");
ac.tset.onclick = ac.set;
ac.treset.onclick = ac.reset;
// (B4) ALARM SOUND
ac.sound = new Audio("wake-up-sound.mp3");
// (B5) START THE CLOCK
ac.alarm = null;
setInterval(ac.tick, 1000);
},
// (C) SUPPORT FUNCTION - CREATE SELECTOR FOR HR, MIN, SEC
createSel : max => {
let selector = document.createElement("select"), opt;
for (let i=0; i<=max; i++) {
opt = document.createElement("option");
i = ac.padzero(i);
opt.value = i;
opt.innerHTML = i;
selector.appendChild(opt);
}
return selector;
},
// (D) SUPPORT FUNCTION - PREPEND HR, MIN, SEC WITH 0 (IF < 10)
padzero : num => {
if (num < 10) { num = "0" + num; }
else { num = num.toString(); }
return num;
},
// (E) UPDATE CURRENT TIME
tick : () => {
// (E1) CURRENT TIME
let now = new Date(),
hr = ac.padzero(now.getHours()),
min = ac.padzero(now.getMinutes()),
sec = ac.padzero(now.getSeconds());
// (E2) UPDATE HTML CLOCK
ac.chr.innerHTML = hr;
ac.cmin.innerHTML = min;
ac.csec.innerHTML = sec;
// (E3) CHECK AND SOUND ALARM
if (ac.alarm != null) {
now = hr + min + sec;
if (now == ac.alarm && ac.sound.paused) { ac.sound.play(); }
}
},
// (F) SET ALARM
set : () => {
ac.alarm = ac.thr.value + ac.thm.value + ac.ths.value;
ac.thr.disabled = true;
ac.thm.disabled = true;
ac.ths.disabled = true;
ac.tset.disabled = true;
ac.treset.disabled = false;
},
// (G) RESET ALARM
reset : () => {
if (!ac.sound.paused) { ac.sound.pause(); }
ac.alarm = null;
ac.thr.disabled = false;
ac.thm.disabled = false;
ac.ths.disabled = false;
ac.tset.disabled = false;
ac.treset.disabled = true;
}
};
// (H) START CLOCK ON PAGE LOAD
window.addEventListener("load", ac.init);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment