Countdown


SUBMITTED BY: Guest

DATE: May 19, 2013, 3:55 a.m.

FORMAT: Text only

SIZE: 2.6 kB

HITS: 2258

  1. <head>
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <title>Untitled Document</title>
  4. <script type="text/javascript">
  5. var interval;
  6. var minutes = 5;
  7. var seconds = 10;
  8. window.onload = function() {
  9. countdown('countdown');
  10. }
  11. function countdown(element) {
  12. interval = setInterval(function() {
  13. var el = document.getElementById(element);
  14. if(seconds == 0) {
  15. if(minutes == 0) {
  16. alert(el.innerHTML = "countdown's over!");
  17. clearInterval(interval);
  18. return;
  19. } else {
  20. minutes--;
  21. seconds = 60;
  22. }
  23. }
  24. if(minutes > 0) {
  25. var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
  26. } else {
  27. var minute_text = '';
  28. }
  29. var second_text = seconds > 1 ? 'seconds' : 'second';
  30. el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
  31. seconds--;
  32. }, 1000);
  33. }
  34. </script>
  35. </head>
  36. <body>
  37. <div id='countdown'></div>
  38. </body>
  39. </html>
  40. <input type="button" onclick="countdown('countdown');" value="Start" />
  41. <input type="button" onclick="minutes=5;seconds=10;" value="Reset" />
  42. window.onload = function() {
  43. countdown('countdown');
  44. }
  45. <a href="javascript:void(0);" onclick="countdown('countdown')">Click Me to Start</a>
  46. var interval;
  47. <a href="javascript:void(0);" onclick="clearInterval(interval)">Click Me to Stop</a>
  48. <input type="button" value="reset" id="reset" />
  49. <input type="button" value="start" id="start" />
  50. var reset = document.getElementById('reset');
  51. reset.onclick = function() {
  52. minutes = 5;
  53. seconds = 10;
  54. clearInterval(interval);
  55. interval = null;
  56. }
  57. var start = document.getElementById('start');
  58. start.onclick = function() {
  59. if (!interval) {
  60. countdown('countdown');
  61. }
  62. }

comments powered by Disqus