Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    34
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Countdown, but not in textbox

    Hi,

    Does anyone have a countdown script that just shows the countdown on a standard page, rather than being inside an input textbox? I've found quite a few countdowns (such as http://www.java-scripts.net/javascri...wn-Timer.phtml & http://javascript.internet.com/navig...-redirect.html) but they all display the countdown in a form textbox.

    Many thanks

    Ben

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by bjblackmore View Post
    Hi,

    Does anyone have a countdown script that just shows the countdown on a standard page, rather than being inside an input textbox? I've found quite a few countdowns (such as http://www.java-scripts.net/javascri...wn-Timer.phtml & http://javascript.internet.com/navig...-redirect.html) but they all display the countdown in a form textbox.

    Many thanks

    Ben
    Ok get one that works in a text box and this is the exsample code

    document.clockform.clock.value = time;

    and you have a text box called clock in a form called clockform

    SO remove the form and text box put a div in with the ID clock

    And then javascript uses
    document.getElementById("clock").innerHTML = time;

    If you get me a script that used a text box i will do it for you
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Like this:-

    Code:
    <div id = "dd2"></div>
    
    <script type = "text/javascript"> 
    var milisec = 0; 
    var seconds = 30; 
    
    function display() { 
    if (milisec <=0) { 
    milisec = 9; 
    seconds -= 1; 
    } 
    if (seconds<=-1) { 
    milisec = 0;
    seconds += 1; 
     } 
    else {
    milisec -= 1;
    }
    
    var result = seconds + "." + milisec;
    document.getElementById("dd2").innerHTML = result;
    
    setTimeout("display()",100);
     
    } 
    
    display(); 
    
    </script>

    Spotted in a safari park: ELEPHANTS PLEASE STAY IN YOUR CAR

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by Philip M View Post
    Like this:-

    Code:
    <div id = "dd2"></div>
    
    <script type = "text/javascript"> 
    var milisec = 0; 
    var seconds = 30; 
    
    function display() { 
    if (milisec <=0) { 
    milisec = 9; 
    seconds -= 1; 
    } 
    if (seconds<=-1) { 
    milisec = 0;
    seconds += 1; 
     } 
    else {
    milisec -= 1;
    }
    
    var result = seconds + "." + milisec;
    document.getElementById("dd2").innerHTML = result;
    
    setTimeout("display()",100);
     
    } 
    
    display(); 
    
    </script>

    Spotted in a safari park: ELEPHANTS PLEASE STAY IN YOUR CAR
    Yea just like that, thanks Phil just one thing,
    display(); - don't do this as most people use script in there header tag, and this would create an error as the div might not have been drawn by that point

    So in the body tag onload="display();"
    Last edited by barkermn01; 12-17-2008 at 04:33 PM.
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • #5
    New Coder
    Join Date
    Dec 2008
    Posts
    34
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ah <div id>!
    I spent ages trying to figure out what would be used as the place holder instead of the text box!
    Thanks guys, thats works beautifully!
    Ben

  • #6
    New Coder
    Join Date
    Dec 2008
    Posts
    34
    Thanks
    5
    Thanked 0 Times in 0 Posts
    One last question, if I wanted to put this in an external .js file, where would you but the milisec & seconds var? I've created the function, and can call it from my html page, but when you put the var milisec & var seconds inside, the countdown stops:

    countdown.js:

    Code:
    function display(){ 
    
     var milisec=0 
     var seconds=10
    
     if (milisec<=0){ 
        milisec=9 
        seconds-=1 
     } 
     if (seconds<=-1){ 
        milisec=0 
        seconds+=1 
     } 
     else {
        milisec-=1;
    }
    	var result = seconds
    	document.getElementById("d2").innerHTML = result;
        setTimeout("display()",100);
    }
    HTML:

    Code:
    <script type="text/javascript" language="javascript" src="countdown.js"></script>
    <body onload="display();">
    Lift off in: <div id="d2"></div>
    Thanks again

    Ben

  • #7
    New Coder
    Join Date
    Dec 2008
    Posts
    34
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ah, ok, it seems to work moving the var milisec=0 & var seconds=10 outside of the function, just above "function display()" are they OK sitting there? For some reason I thought the vars had to go inside the function.

    Thanks

    Ben

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by bjblackmore View Post
    Ah, ok, it seems to work moving the var milisec=0 & var seconds=10 outside of the function, just above "function display()" are they OK sitting there? For some reason I thought the vars had to go inside the function.

    Thanks

    Ben
    The script should (and does) work perfectly well as it is.

    If
    var milisec = 0;
    var seconds = 10;

    are within the function then the variables are local to that function, which is best here.
    If they are placed outside the function then they are global variables, that is available to any other function.

    But the script ought to work either way. If it doesn't then you have made some sort of mistake in copying to your .js file.

  • #9
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    The other way to keep it to that function,
    PHP Code:
    function display(){ 

     var 
    milisec=
     
    var seconds=10 
    To This code
    PHP Code:
     function display(milisecseconds){ 
    Then when you call it just do display(0,10);

    This would make it safer and easier to call stop var's from being global and still alows you to chnage timer with out changin your .js file
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)


  •  

    Posting Permissions

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