JavaScript - Simple Alarm Clock

Submitted by: 
Visitors have accessed this post 428 times.

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...

Before we started:

And this is the link for the bootstrap that has been used for the layout of the application

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.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Simple Alarm Clock</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <input type="datetime-local" id="alarmTime"/>
  18. <button type="button" onclick="setAlarm(this);" id="alarmButton" class="btn btn-success"><span class="glyphicon glyphicon-time"></span> Set Alarm</button>
  19. </center>
  20. <br />
  21. <div id="selectButton" style="display:none;">
  22. <button onclick="snoozeAlarm();" class="btn btn-warning"><span class="glyphicon glyphicon-pause"></span> Snooze 5 minutes</button>
  23. <button onclick="stopAlarm();" class="btn btn-danger"><span class="glyphicon glyphicon-stop"></span> Stop Alarm</button>
  24. </center>
  25. </div>
  26. </div>
  28. <script src="js/script.js"></script>
  29. </body>
  30. </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.

  1. var sound = new Audio();
  2. sound.src = 'alarm.mp3';
  3. var timer;
  5. function setAlarm(el){
  6. var time = document.getElementById('alarmTime').valueAsNumber;
  7. if(isNaN(time)){
  8. alert("Invalid DateTime");
  9. return;
  10. }
  12. var alarm = new Date(time);
  13. var alarmTime = new Date(alarm.getUTCFullYear(), alarm.getUTCMonth(), alarm.getUTCDate(), alarm.getUTCHours(), alarm.getUTCMinutes(), alarm.getUTCSeconds());
  14. var duration = alarmTime.getTime() - (new Date()).getTime();
  16. if(duration < 0){
  17. alert('Time is already passed');
  18. return;
  19. }
  21. timer = setTimeout(initAlarm, duration);
  22. el.innerHTML = "<span class='glyphicon glyphicon-remove'></span> Cancel Alarm";
  23. el.setAttribute('onclick', 'cancelAlarm(this);');
  24. el.setAttribute('class', 'btn btn-danger');
  25. }
  28. function cancelAlarm(el){
  29. el.innerHTML = "<span class='glyphicon glyphicon-time'></span> Set Alarm";
  30. el.setAttribute('onclick', 'setAlarm(this);');
  31. el.setAttribute('class', 'btn btn-success');
  32. clearTimeout(timer);
  33. }
  35. function initAlarm(){
  36. sound.loop = true;
  38. document.getElementById('alarmButton').style.display = 'none';
  39. document.getElementById('selectButton').style.display = '';
  40. }
  42. function stopAlarm(){
  43. sound.pause();
  44. sound.currentTime = 0;
  45. document.getElementById('selectButton').style.display = 'none';
  46. cancelAlarm(document.getElementById('alarmButton'));
  47. document.getElementById('alarmButton').style.display = '';
  48. }
  50. function snoozeAlarm(){
  51. stopAlarm();
  52. setTimeout(initAlarm, 300000);
  53. button.innerText = "Cancel Alarm";
  54. button.setAttribute('onclick', 'cancelAlarm(this);');
  55. }

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!!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.