JavaScript - Christmas Countdown Timer

In this tutorial we will create a Christmas Countdown Timer using JavaScript. This code will start the countdown for christmas eve when the user click the start button. The code use onclick() function to launch a specific function that will start a countdown by assigning the startCountDownTimer() function as a parameter in setTimeOut() javascipt function. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

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">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 - Christmas Countdown Timer</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <button onclick="startCountDownTimer()" type="button" class="btn btn-sm btn-success">Start Countdown</button>
  17.                 <center><h1 id="title">Christmas COUNTDOWN</h1></center>
  18.                 <table class="table">
  19.                         <tr style="background-color:#000;">
  20.                                 <td style="color:#0f0;" id="days"><center><h1>0</h1></center></td>
  21.                                 <td style="color:#0f0;" id="hrs"><center><h1>00</h1></center></td>
  22.                                 <td style="color:#0f0;" id="min"><center><h1>00</h1></center></td>
  23.                                 <td style="color:#0f0;" id="sec"><center><h1>00</h1></center></td>
  24.                         </tr>
  25.                         <tr>
  26.                                 <td><center><h2>Days</h2></center></td>
  27.                                 <td><center><h2>Hours</h2></center></td>
  28.                                 <td><center><h2>Minutes</h2></center></td>
  29.                                 <td><center><h2>Seconds</h2></center></td>
  30.                         </tr>
  31.                 </table>
  32.         </div>
  33.         <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will start the countdown when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function stopStimer(){
  2.         document.getElementById('title').innerHTML = "HAPPY NEW YEAR!";
  3.         document.getElementById('days').innerHTML = "<center><h1>00</h1></center>";
  4.         document.getElementById('hrs').innerHTML = "<center><h1>00</h1></center>";
  5.         document.getElementById('min').innerHTML = "<center><h1>00</h1></center>";
  6.         document.getElementById('sec').innerHTML = "<center><h1>00</h1></center>";
  7. }
  8.  
  9. function startCountDownTimer(){
  10.         var today = new Date();
  11.         var target = new Date("December 25, 2019 00:00:00");
  12.         var currentTime = today.getTime();
  13.         var targetTime = target.getTime();
  14.        
  15.         var time = targetTime - currentTime;
  16.        
  17.         var sec = Math.floor(time/1000);
  18.         var min = Math.floor(sec/60);
  19.         var hrs = Math.floor(min/60);
  20.         var days = Math.floor(hrs/24);
  21.        
  22.         hrs = hrs % 24;
  23.         min = min % 60;
  24.         sec = sec % 60;
  25.        
  26.        
  27.         hrs = (hrs<10) ? "0"+hrs : hrs;
  28.         min = (min<10) ? "0"+min : min;
  29.         sec = (sec<10) ? "0"+sec : sec;
  30.        
  31.         document.getElementById('days').innerHTML = "<center><h1>"+days+"</h1></center>";
  32.         document.getElementById('hrs').innerHTML = "<center><h1>"+hrs+"</h1></center>";
  33.         document.getElementById('min').innerHTML = "<center><h1>"+min+"</h1></center>";
  34.         document.getElementById('sec').innerHTML = "<center><h1>"+sec+"</h1></center>";
  35.        
  36.         if(time > 0){
  37.                 setTimeout(startCountDownTimer, 1000);
  38.         }else{
  39.                 stopStimer();
  40.         }
  41. }
There you have it we successfully created a Christmas Countdown 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