Posted By

reverend on 04/01/11


Tagged

timer count-down


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

notturnale
Priestd09


Javascript count down timer in minutes and seconds


 / Published in: JavaScript
 

  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
Posted By: mikeyd on July 16, 2012

How do you stop the counter once it reaches zero? I embedded this code and once it reaches zero it starts to count down negatively for example (-1min 30 seconds). How can I get the counter to STOP at zero?

Thank You

Posted By: niranjanyadavb on June 5, 2014

please help me out to stop the time at 0 minutes and 0 seconds

thanking you

You need to login to post a comment.