LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Pure JavaScript Digital Alarm Clock With Alarm Sound

Pure JavaScript Digital Alarm Clock With Alarm Sound

Pure-JavaScript-Digital-Alarm-Clock-With-Alarm-Sound
This JavaScript code snippet helps you to create an alarm clock with sound. It comes with a digital clock that shows the current time and alarm settings interface. Users can easily set an alarm for a certain time, when the alarm starts, the user can turn it off or snooze for the next five minutes.

You can integrate this alarm code snippet in your project as a reminder or allow users to set an alarm for a certain time. Similarly, it can also be used in quiz app with timer or limited-time offers to alert the users with sound.
There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure CSS.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.
Recommended For You:
Secure Your Web Pages To Be Embedded Via HTML FRAMES

How To Create Pure JavaScript Digital Alarm Clock With Alarm Sound?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<!-- Normalize CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<!-- Orbitron Fonts CSS -->
<link href="https://fonts.googleapis.com/css?family=Orbitron" rel="stylesheet">
<style type="text/css">
.clock-wrapper{
background: #333;
line-height: 2;
text-align:center;
}

/* header */
.clock-wrapper h2 {
font-family: 'Orbitron', sans-serif;
font-size: 4em;
color: #fff;
}

/* buttons */
.clock-wrapper button {
cursor: pointer;
margin: 0 5px;
padding: 10px 30px;
background: transparent;
border: 1px solid #ccc;
border-radius: 8px;
outline: 0;
color: #fff;
transition: all ease 300ms;
margin: 10px 0;
}
.clock-wrapper button:hover {
color: #333;
background: #fff;

}

/* center content vertically and horizontally */
.table {
display: table;
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
cursor: default;
}

/* variable misc classes */
.hide {
display: none;
} 
</style>

HTML:

<div class="table clock-wrapper">
<div class="cell">
<h2 id="clock" class="clock"></h2>

<select id="hours"></select>
<select id="minutes"></select>
<select id="seconds"></select>
<select id="ampm">
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>

<p>
<button id="snooze" class="hide">Snooze</button>
<button id="startstop">Set Alarm</button>
</p>
</div>
</div>

JavaScript:

<script type="text/javascript">
// set our variables
var time, alarm, currentH, currentM,
activeAlarm = false,
sound = new Audio("https://freesound.org/data/previews/316/316847_4939433-lq.mp3");

/*
audio sound source: https://freesound.org/people/SieuAmThanh/sounds/397787/
*/

// loop alarm
sound.loop = true;

// define a function to display the current time
function displayTime() {
var now = new Date();
time = now.toLocaleTimeString();
clock.textContent = time;
// time = "1:00:00 AM";
// watch for alarm
if (time === alarm) {
sound.play();

// show snooze button
snooze.className = "";
}
setTimeout(displayTime, 1000);
}
displayTime();

// add option values relative towards time
function addMinSecVals(id) {
var select = id;
var min = 59;

for (i = 0; i <= min; i++) {
// defined as new Option(text, value)
select.options[select.options.length] = new Option(i < 10 ? "0" + i : i, i < 10 ? "0" + i : i);
}
}
function addHours(id) {
var select = id;
var hour = 12;

for (i = 1; i <= hour; i++) {
// defined as new Option(text, value)
select.options[select.options.length] = new Option(i < 10 ? "0" + i : i, i);
}
}
addMinSecVals(minutes);
addMinSecVals(seconds);
addHours(hours);

// set and clear alarm
startstop.onclick = function() {
// set the alarm
if (activeAlarm === false) {
hours.disabled = true;
minutes.disabled = true;
seconds.disabled = true;
ampm.disabled = true;

alarm = hours.value + ":" + minutes.value + ":" + seconds.value + " " + ampm.value;
this.textContent = "Clear Alarm";
activeAlarm = true;
} else {
// clear the alarm
hours.disabled = false;
minutes.disabled = false;
seconds.disabled = false;
ampm.disabled = false;

sound.pause();
alarm = "00:00:00 AM";
this.textContent = "Set Alarm";

// hide snooze button
snooze.className = "hide";
activeAlarm = false;
}
};

// snooze for 5 minutes
snooze.onclick = function() {
if (activeAlarm === true) {
// grab the current hour and minute
currentH = time.substr(0, time.length - 9);
currentM = time.substr(currentH.length + 1, time.length - 8);

if (currentM >= "55") {
minutes.value = "00";
hours.value = parseInt(currentH) + 1;
} else {
if (parseInt(currentM) + 5 <= 9) {
minutes.value = "0" + parseInt(currentM + 5);
} else {
minutes.value = parseInt(currentM) + 5;
}
}

// hide snooze button
snooze.className = "hide";

// now reset alarm
startstop.click();
startstop.click();
} else {
return false;
}
};
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
How To Create An HTML Hover Button For Your Blog & Website

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *