JavaScript - Simple Alarm Clock
Submitted by razormist on Thursday, January 3, 2019 - 18:17.
In this tutorial we will create a Simple Alarm Clock using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It can renders web pages in an interactive and dynamic fashion. It is widely used in designing a stunning website. This code can be used as your converter to your mathematical problem. So Let's do the coding...
There you have it we successfully created a Simple Alarm Clock
using javascript. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Before we started:
And this is the link for the bootstrap that has been used for the layout of the application https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. This code will render application and display the form. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <center>
- <input type="datetime-local" id="alarmTime"/>
- </center>
- <br />
- <div id="selectButton" style="display:none;">
- <center>
- </center>
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will trigger an alarm sound when the set time reach the target time. To do this just copy write the code inside the text editor and save it inside the js directory as script.js.- var sound = new Audio();
- sound.src = 'alarm.mp3';
- var timer;
- function setAlarm(el){
- var time = document.getElementById('alarmTime').valueAsNumber;
- if(isNaN(time)){
- alert("Invalid DateTime");
- return;
- }
- var alarm = new Date(time);
- var alarmTime = new Date(alarm.getUTCFullYear(), alarm.getUTCMonth(), alarm.getUTCDate(), alarm.getUTCHours(), alarm.getUTCMinutes(), alarm.getUTCSeconds());
- var duration = alarmTime.getTime() - (new Date()).getTime();
- if(duration < 0){
- alert('Time is already passed');
- return;
- }
- timer = setTimeout(initAlarm, duration);
- el.innerHTML = "<span class='glyphicon glyphicon-remove'></span> Cancel Alarm";
- el.setAttribute('onclick', 'cancelAlarm(this);');
- el.setAttribute('class', 'btn btn-danger');
- }
- function cancelAlarm(el){
- el.innerHTML = "<span class='glyphicon glyphicon-time'></span> Set Alarm";
- el.setAttribute('onclick', 'setAlarm(this);');
- el.setAttribute('class', 'btn btn-success');
- clearTimeout(timer);
- }
- function initAlarm(){
- sound.loop = true;
- sound.play();
- document.getElementById('alarmButton').style.display = 'none';
- document.getElementById('selectButton').style.display = '';
- }
- function stopAlarm(){
- sound.pause();
- sound.currentTime = 0;
- document.getElementById('selectButton').style.display = 'none';
- cancelAlarm(document.getElementById('alarmButton'));
- document.getElementById('alarmButton').style.display = '';
- }
- function snoozeAlarm(){
- stopAlarm();
- setTimeout(initAlarm, 300000);
- button.innerText = "Cancel Alarm";
- button.setAttribute('onclick', 'cancelAlarm(this);');
- }
Add new comment
- 6394 views