banner freezing after some modifications

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
vin_akleh
Forum Commoner
Posts: 53
Joined: Sat Feb 14, 2009 10:26 am

banner freezing after some modifications

Post by vin_akleh »

i have a banner that changes image every couple of seconds that retrieve the images from a static images like so:
old code:

Code: Select all

var image1=new Image()
image1.src="uploads/firstcar.gif"
var image2=new Image()
image2.src="uploads/secondcar.gif"
var image3=new Image()
image3.src="uploads/thirdcar.gif"
var image4=new Image()
image3.src="uploads/cat.gif"

Code: Select all

<div class="banner">
<p class="banner_image">
<a href="http://www.w3.org/">
<img src="banner/123.jpg" name="slide" width="290px">
<script>
<!--
//variable that will increment through the images
var step=1
function slideit(){
//if browser does not support the image object, exit.
if (!document.images)
return
document.images.slide.src=eval("image"+step+".src")
if (step<3)
step++
else
step=1
//call function "slideit()" every 2.5 seconds
setTimeout("slideit()",2500)
}
slideit()
//-->
</script>
</p>
<p class="banner">
<a href="http://www.w3.org/">banner</a>
</p>
</div>
know i want it to retrieve the images from database like so
New modifications:

Code: Select all

<?php $count_im=1;
 $req=mysql_query("select * from banner");
	while ($res=mysql_fetch_array($req)){ ?>
var image<?php echo $count_im; ?> =new Image()
image<?php echo $count_im; ?>.src="<?php echo $res["banner"]; ?>"
<?php $count_im++;
}?>
but the problem is that it know freezes on the last image, it doesn't loop!! any ideas how to fix it
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: banner freezing after some modifications

Post by califdon »

Stop and think about it. PHP is executed by the web server before it sends the page to the browser. Once the page is sent to the browser, the JS function is stuck with whatever data was in the HTML page that the server sent. If you want to go back to the server for more data, you need to use Ajax, in connection with JS.
Post Reply