JavaScript - Simple Countdown Timer Using AngularJS

In this tutorial we will create a Simple Anchor Scroll using AngularJS. AngularJS is a JavaScript-based open-source front-end web application framework . It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you will need to download & install the AngularJS here's the link https://angularjs.org/.

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <head>
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scal=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">JavaScript - Simple Countdown Timer Using AngularJS</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15. <div class='wrap' ng-app='app'>
  16. <div class='time-to'>
  17. <center style="font-size:50px;">Christmas Countdown</center>
  18. <br />
  19. <center><span style="font-size:40px; color:red;" countdown='' date='December 25, 2019 00:00:00'></span></center>
  20. <center style="font-size:25px; color:blue;">Before Christmas</center>
  21. </div>
  22. </div>
  23. </div>
  24. <script src="js/angular.js"></script>
  25. <script src="js/script.js"></script>
  26. </body>
  27. </html>

Creating the Script

This code contains the main function of the application. This code will start counting when the web page is start. To that just kindly copy and write these block of codes inside the text editor, then save it as script.js
  1. (function() {
  2. var app;
  3. app = angular.module('app', []);
  4. app.directive('countdown', [
  5. 'Util', '$interval', function(Util, $interval) {
  6. return {
  7. restrict: 'A',
  8. scope: {
  9. date: '@'
  10. },
  11. link: function(scope, element) {
  12. var future;
  13. future = new Date(scope.date);
  14. $interval(function() {
  15. var diff;
  16. diff = Math.floor((future.getTime() - new Date().getTime()) / 1000);
  17. return element.text(Util.dhms(diff));
  18. }, 1000);
  19. }
  20. };
  21. }
  22. ]);
  23. app.factory('Util', [
  24. function() {
  25. return {
  26. dhms: function(t) {
  27. var days, hours, minutes, seconds;
  28. days = Math.floor(t / 86400);
  29. t -= days * 86400;
  30. hours = Math.floor(t / 3600) % 24;
  31. t -= hours * 3600;
  32. minutes = Math.floor(t / 60) % 60;
  33. t -= minutes * 60;
  34. seconds = t % 60;
  35. return [days + 'd', hours + 'h', minutes + 'm', seconds + 's'].join(' ');
  36. }
  37. };
  38. }
  39. ]);
  40. }).call(this);
There you have it we successfully created a Simple Countdown Timer using AngularJS. 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!!

Comments

Add new comment