Javascript Countdown

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
Parody
Forum Contributor
Posts: 252
Joined: Fri May 06, 2005 7:06 pm
Location: Great Britain

Javascript Countdown

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
Post Reply