JavaScript - Simple Timer

In this tutorial we will create a Simple Timer using JavaScript. This code will generate a timer GUI that will start running when the user click the start button. The code use a setTimeout function to initiate a timer() that increment a value continuously after the start button is clicked. This is a free user-friendly kind of program, you can modify it and apply to your working application. We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. 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="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Simple Timer</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <center><label style='font-size:45px;' id="result">00:00:00</label></center>
  19. <center><button id="start" class="btn btn-primary" onClick="start()">Start</button> <button class="btn btn-success" onClick="reset()">Reset</button></center>
  20. </div>
  21. </div>
  22. <script type="text/javascript" src="js/script.js"></script>
  23. </body>
  24. </html>

Creating the Script

This code contains the script of the application. This code will start a timer when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var time = 0;
  2. var running = 0;
  3.  
  4. function start(){
  5. if(running == 0){
  6. running = 1;
  7. timer();
  8. document.getElementById("start").innerHTML = "Pause";
  9. document.getElementById("start").setAttribute('class', 'btn btn-danger');
  10. }else{
  11. running = 0;
  12. document.getElementById("start").innerHTML = "Resume";
  13. document.getElementById("start").setAttribute('class', 'btn btn-warning');
  14. }
  15. }
  16.  
  17. function reset(){
  18. running = 0;
  19. time = 0;
  20. document.getElementById("start").innerHTML = "Start";
  21. document.getElementById("result").innerHTML = "00:00:00";
  22. document.getElementById("start").setAttribute('class', 'btn btn-primary');
  23. }
  24.  
  25. function timer(){
  26. if(running == 1){
  27. setTimeout(function(){
  28. time++;
  29. var mins = Math.floor(time/10/60)%60;
  30. var secs = Math.floor(time/10)%60;
  31. var ms = time%10;
  32.  
  33. if(mins<10){
  34. mins = "0" + mins;
  35. }
  36. if(secs<10){
  37. secs = "0" + secs;
  38. }
  39.  
  40. document.getElementById("result").innerHTML = mins+ ":" +secs+ ":" + "0" +ms;
  41. timer();
  42. },100);
  43. }
  44. }
There you have it we successfully created a Simple Timer using JavaScript. I hope that this 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