FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Countdown Javascript with PHP variables





Rhysige
Ok ive been working on an online game http://warsim.swc.frih.net anyway you dont really need to see this.
The problem I have is I want a javascript timer that will be able to get the time remaining from a PHP variable (which gets it from the database) and then counts down, if the time reaches 0 I want it to redirect to a page or reload the current one either is good. The time needs to contain only Minutes and seconds with the input being in seconds.
JustaMin
Try this:

Code:

<?
$seconds = 360;    //Number of seconds, get it from db and store it in this var
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Countdown</title>
<head>
</head>
<body>

<script language="JavaScript">
var countDownInterval=<?=$seconds?>;
var c_reloadwidth=200
</script>
<ilayer id="c_reload" width=&{c_reloadwidth}; ><layer id="c_reload2" width=&{c_reloadwidth}; left=0 top=0></layer></ilayer>
<script>
var countDownTime=countDownInterval+1;
function countDown(){
countDownTime--;
if (countDownTime <=0){
countDownTime=countDownInterval;
clearTimeout(counter)
window.location.href="http://your.url.com" //Redirection URL
return
}
if (document.all) //if IE 4+
document.all.countDownText.innerText = countDownTime+" ";
else if (document.getElementById) //else if NS6+
document.getElementById("countDownText").innerHTML=countDownTime+" "
else if (document.layers){
document.c_reload.document.c_reload2.document.write('Redirect in <span id="countDownText">'+countDownTime+' </span> seconds')
document.c_reload.document.c_reload2.document.close()
}
counter=setTimeout("countDown()", 1000);
}

function startit(){
if (document.all||document.getElementById)
document.write('Redirect in <span id="countDownText">'+countDownTime+' </span> seconds')
countDown()
}

if (document.all||document.getElementById)
startit()
else
window.onload=startit
</script>

</body>
</html>
JustaMin
If you want it to display minutes and seconds as opposed to just seconds, change the script to this:

Code:

<script language="JavaScript">
var countDownInterval=<?=$seconds?>;
var c_reloadwidth=200
</script>
<ilayer id="c_reload" width=&{c_reloadwidth}; ><layer id="c_reload2" width=&{c_reloadwidth}; left=0 top=0></layer></ilayer>
<script>
var countDownTime=countDownInterval+1;
function countDown(){
countDownTime--;
if (countDownTime <=0){
countDownTime=countDownInterval;
clearTimeout(counter)
window.location.href="http://your.url.com" //Redirection URL
return
}
var mins = Math.floor(countDownTime/60)
var secs = countDownTime-(mins*60)
if (document.all) //if IE 4+
document.all.countDownText.innerText = mins+" minutes "+secs+ " ";
else if (document.getElementById) //else if NS6+
document.getElementById("countDownText").innerHTML=mins+" minutes "+secs+ " "
else if (document.layers){
document.c_reload.document.c_reload2.document.write('Redirect in <span id="countDownText">'+countDownTime+' </span> seconds')
document.c_reload.document.c_reload2.document.close()
}
counter=setTimeout("countDown()", 1000);
}

function startit(){
if (document.all||document.getElementById)
document.write('Redirect in <span id="countDownText">'+countDownTime+' </span> seconds')
countDown()
}

if (document.all||document.getElementById)
startit()
else
window.onload=startit
</script>

mattd8752
That game is awsome, what is it coded in php, javascript, but what is it that does the different cursors, did you do the whole thing in javascript?
Rhysige
I cant take sole credit for it, a friend of mine wrote alot of the GUI stuff I just work on features like marine boarding and Hyperspace Smile

Thanks guys looks like that will do the job Very Happy
Arcath
i have a script that works pretty well, go to www.arcath.net to see it in action (u may need to scroll down)

tell me and i will post it
JustaMin
You've spelled 'coming' wrong...
Also your script oesnt redirect, use a database or count just minutes and seconds Smile
Related topics
php variables
[DESIGNER] - Sites de Código HTML, JAVASCRIPT, PHP(PORUGUÊS)
Guissmo's Javascript (1)
XML and Javascript
which one programming lanuge result's the best speed
A very good PHP MySQL Tutorial
[man]Azlo tu mismo ShoutBox en PHP y mysql
AJAX tutorial [2nd part now updated]
[soft]Lista de Códigos JavaScript
Whos is the FUTURE.... JAVA or .NET
Layout Gap PRoblems with PHP/Javascript Combo
Browser close run a php script.
Javascript and Dynamic Select Boxes
javascript php html styling and good practice.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.