Page 1 of 1

Javascript Countdown

Posted: Fri Jun 24, 2005 11:02 am
by Parody
I have a value in php that i need to be the starting point of a countdown in javascript, I have the code for the countdown but after researching found that javascript and php don't get on :lol:

I also found out that sessions are the only way to send and recieve values between languages.
My question is:
How do I recieve session info in javascript?

Posted: Fri Jun 24, 2005 12:09 pm
by Chris Corbyn
PHP and JavaScript can get on quite nicely actually...

This sounds good and easy to demonstrate to me too....

Code: Select all

<?php

function displayTimer($seconds) {

echo <<<EOD
<script type="text/javascript">
<!-- Hide JS from old browsers
var secs = {$seconds};

function doTimer(s) {
    if (s > 0) {
        document.getElementById('timer').innerHTML = s-1;
        window.setTimeout(function() { doTimer(s-1); }, 1000);
    } else {
        window.clearTimeout();
    }
}

document.write('<span id="timer">' + {$seconds} + '</span>');

doTimer(secs);
// End -->
</script>
EOD;

}
?>
Then you can call your nice shiny JS timer using PHP :P

Code: Select all

<body>
Yadda yadda
<br />
Look it's a timer!
<p />
<?php
displayTimer(20);
?>
<br />
Yadda yadda