...

View Full Version : Countdown, but not in textbox



bjblackmore
12-17-2008, 03:45 PM
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/javascripts/Countdown-Timer.phtml & http://javascript.internet.com/navigation/countdown-redirect.html) but they all display the countdown in a form textbox.

Many thanks

Ben

barkermn01
12-17-2008, 04:17 PM
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/javascripts/Countdown-Timer.phtml & http://javascript.internet.com/navigation/countdown-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

Philip M
12-17-2008, 04:19 PM
Like this:-


<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

barkermn01
12-17-2008, 04:30 PM
Like this:-


<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();"

bjblackmore
12-17-2008, 04:40 PM
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

bjblackmore
12-17-2008, 04:50 PM
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:


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:


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

Thanks again

Ben

bjblackmore
12-17-2008, 04:58 PM
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

Philip M
12-17-2008, 06:42 PM
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.

barkermn01
12-18-2008, 12:20 PM
The other way to keep it to that function,


function display(){

var milisec=0
var seconds=10

To This code


function display(milisec, seconds){


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum