Javascript count down timer in minutes and seconds


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <html>
  2. <head>
  3. <title>Countdown</title>
  4. <script type="text/javascript">
  5. // set minutes
  6. var mins = 5;
  7.  
  8. // calculate the seconds (don't change this! unless time progresses at a different speed for you...)
  9. var secs = mins * 60;
  10. function countdown() {
  11. setTimeout('Decrement()',1000);
  12. }
  13. function Decrement() {
  14. if (document.getElementById) {
  15. minutes = document.getElementById("minutes");
  16. seconds = document.getElementById("seconds");
  17. // if less than a minute remaining
  18. if (seconds < 59) {
  19. seconds.value = secs;
  20. } else {
  21. minutes.value = getminutes();
  22. seconds.value = getseconds();
  23. }
  24. secs--;
  25. setTimeout('Decrement()',1000);
  26. }
  27. }
  28. function getminutes() {
  29. // minutes is seconds divided by 60, rounded down
  30. mins = Math.floor(secs / 60);
  31. return mins;
  32. }
  33. function getseconds() {
  34. // take mins remaining (as seconds) away from total seconds remaining
  35. return secs-Math.round(mins *60);
  36. }
  37. </script>
  38. </head>
  39. <body>
  40.  
  41. <div id="timer">
  42. This is only valid for the next <input id="minutes" type="text" style="width: 14px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> minutes and <input id="seconds" type="text" style="width: 26px; border: none; background-color:none; font-size: 16px; font-weight: bold;"> seconds.
  43. </div>
  44. <script>
  45. countdown();
  46. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.