Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Countdown Timer

  1. #1
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Countdown Timer

    I found this countdown timer on the internet.

    It starts from 30 seconds and counts down to 0

    What I would like for it to do is once it hits 0 to automatically
    reset to 30 and stop without using page refresh or a button to reset.

    Any help would be appreciated.


    Code:
    <form name="counter"><input type="text" size="8" 
    name="d2"></form> 
    
    <script> 
    <!-- 
    // 
     var milisec=0 
     var seconds=30 
     document.counter.d2.value='30' 
    
    function display(){ 
     if (milisec<=0){ 
        milisec=9 
        seconds-=1 
     } 
     if (seconds<=-1){ 
        milisec=0 
        seconds+=1 
     } 
     else 
        milisec-=1 
        document.counter.d2.value=seconds+"."+milisec 
        setTimeout("display()",100) 
    } 
    display() 
    --> 
    </script>

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there chriscc17,

    that is a very old script and has a few errors.

    The worst being that it carried on running after reaching zero.

    Here is modified version with your requirement included...
    Code:
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>simple countdown</title>
    
    <style type="text/css">
    form {
        text-align:center;
     }
    input {
        font-family:monospace;
        font-size:18px;
        font-weight:bold;
        text-align:center;
        background-color:#f0f0f0;
     }
    </style>
    
    <script type="text/javascript">
    
    /*** these values are editable ***/
    
       var milisec=0; 
       var seconds=30; 
    
    /********************************/
    
       var zero=null;
       var temp=seconds+':'+milisec+'0';
    
    function display(){
    if(seconds<10){
       zero='0';
     }
       document.forms[0][0].value=zero+seconds+':'+milisec+'0';
    if(milisec<=0){ 
       milisec=10;
       seconds--;
     } 
    if(seconds<0){
       setTimeout(function(){document.forms[0][0].value=temp},1000);
       return;
     } 
       milisec-- 
       setTimeout(function(){display()},100)  
     }
       window.addEventListener?
       window.addEventListener('load',display,false):
       window.attachEvent('onload',display);
    </script>
    
    </head>
    <body>
    
    <form action="#">
     <div>
      <input type="text" size="8">
     </div>
    </form> 
    
    </body>
    </html>
    
    coothead
    Last edited by coothead; 03-30-2011 at 09:28 AM.

  3. The Following User Says Thank You to coothead For This Useful Post:

    chriscc17 (03-30-2011)

  4. #3
    Join Date
    Feb 2011
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    Thanks a lot. That's exactly what I was looking for.

  5. #4
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    No problem, you're very welcome.

  6. #5
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi there, I know this is an old thread but I do need this particular code to work for my needs and any help is greatly appreciated. Is there a way to add a minute part to it? I need this exact script but need it to count down from 7minutes 30seconds. Thank you for your help.

  7. #6
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there jpalmes,

    here is a possible example...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    <meta charset="utf-8">
    
    <title>simple countdown</title>
    
    <style>
    form {
        text-align:center;
     }
    input {
        font-family:'courier new',monospace;
        font-size:18px;
        font-weight:bold;
        text-align:center;
        background-color:#f0f0f0;
        box-shadow: 5px 5px 5px #999;                            
     }
    </style>
    
    <script>
    
    /*** these values are editable ***/
    
       var sec=30; 
       var min=7;
    
    /********************************/
    
       var m=0;
       var z='';
       var z1='';
     
    if(sec<=10){
       zero='0';
     }
    if(min<10){
       zero1='0';
     }
       var t=z1+min+':'+z+sec+':'+m+'0';
       m++;
    
    function display(){
    
    if(m<=0){ 
       m=10;
       sec--;
     } 
    if(sec<10){
       z='0';
     }
    if(sec<0){ 
       sec=59;
       z='';
       min--;
     } 
    if(min<10){
       z1='0';
     }
       m-- 
       document.forms[0][0].value=z1+min+':'+z+sec+':'+m+'0'; 
      
    if((min<=0)&&(sec==0)&&(m==0)){
       setTimeout(function(){document.forms[0][0].value=t},1000);
       return;
     }
       setTimeout(function(){display()},100); 
     }
       window.addEventListener?
       window.addEventListener('load',display,false):
       window.attachEvent('onload',display);
    </script>
    
    </head>
    <body>
    
    <form action="#">
     <div>
      <input type="text" size="12">
     </div>
    </form> 
    
    </body>
    </html>
    
    coothead

  8. #7
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    that worked! thank you so much! If its not too much to ask, can you help me with one little thing? How can it be modified to stay at 00:00:00 once the timer ends and not have it reset to the set time? thank you sooo much!

  9. #8
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there jpalmes,

    just remove this line from the code...
    Code:
    setTimeout(function(){document.forms[0][0].value=t},1000);
    
    coothead

  10. #9
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    oh gosh. you're a life saver

    Thank you soooo much

  11. #10
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    No problem, you're very welcome .
    coothead

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •