Run PHP from setTimeout

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcoppo
Forum Newbie
Posts: 2
Joined: Tue Sep 09, 2003 12:57 am

Run PHP from setTimeout

Post by jcoppo »

Can someone explain why executing PHP code from a setTimeout loop does not run after the first call to the function. As an example, the following code will display a random number the first time it is called. But successive calls will not change the PHP output. I know that it is running because the javascript variable Cnt increments ever 1000 milliseconds.

Code: Select all

<HTML><HEAD><TITLE>Test</TITLE>
<SCRIPT type=text/javascript>
var Cnt=0;
function showtime()&#123;
	timerID = setTimeout("showtime()",1000)
	var OUT = document.getElementById('OUT');
	var n = '<?php echo intval(rand(1,20)); ?>';
	Cnt++;
	OUT.innerHTML=n+' '+Cnt;
&#125;
</SCRIPT>
</HEAD>
<BODY onLoad="showtime()">
<DIV id=OUT></DIV>
</BODY></HTML>
I have many situations where I would like to use this technique. For instance, dynamically updating market quotes and displaying news feeds.

Any help would be most appreciated.
jcoppo
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

When the javascript is run, it's allready sendt to the clients browser. Then its to late to run any php-scripts as php is server based.

There is a sticky topic in this forum regarding php and javascript working together.

viewtopic.php?t=1030
jcoppo
Forum Newbie
Posts: 2
Joined: Tue Sep 09, 2003 12:57 am

Post by jcoppo »

That clears it up. You saved me endless hours of frustration.

jcoppo
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

javascript can refresh a page using setTimeout. so if you have an iframe (or frame for that matter) with say a stock quote, a javascipt could refresh the iframe for you.
Post Reply