Guest User

http://stackoverflow.com/questions/29323154/set-an-initial-v

a guest
Apr 2nd, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.13 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <head>
  3. <script type="text/javascript">
  4. var cdtime;
  5. var minutes = 59;
  6. var seconds = 58;
  7.  
  8. function countdown(element) {
  9.     cdtime = setInterval(function() {
  10.         var timer = document.getElementById(element);
  11.         if(seconds == 0) {
  12.             if(minutes == 0) {
  13.                 alert(timer.innerHTML = "countdown's over!");                    
  14.                 clearInterval(cdtime);
  15.                 return;
  16.             } else {
  17.                 minutes--;
  18.                 seconds = 60;
  19.             }
  20.         }
  21.         if(minutes > 0) {
  22.             var minutetxt = minutes + (minutes > 1 ? ' minutes' : 'minute');
  23.         } else {
  24.             var minutetxt = '';
  25.         }
  26.         var secondstxt;
  27.         if(seconds > 1) {
  28.             secondstxt = 'seconds';
  29.         } else {
  30.             secondstxt = 'second';
  31.         }
  32.  
  33.         timer.innerHTML = minutetxt + ' ' + seconds + ' ' + secondstxt;
  34.         seconds--;
  35.     }, 1000);
  36. }
  37. </script>
  38. </head>
  39. <body>
  40. <input type="button" onclick="countdown('countdown')" value="Start" />
  41. <div id='countdown'>59 minutes 59 seconds</div>
  42. </body>
  43. </html>
Add Comment
Please, Sign In to add comment