Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image refresh with javascript

Status
Not open for further replies.

JackTheRussel

Programmer
Aug 22, 2006
110
FI
Hi.

I have tried to create javascript-code which would refresh images that are in my web-page.

I put images on site by using PHP. At the same time when I put images on my site, I put image-names on javascript-array.

Then I have tired to create code, which would refresh images.

// OK Here I put images on site. I get image-names from database. code is shortened:
Code:
for ($i = 0; $i < mysql_num_rows($query); $i++) {
echo "<img src='$record'><br><br>";

// Here I take image-names to the js-array
echo "<script type='text/javascript'>"; 
echo "imagenames[$i] = '$record';";
echo "</script>";

}

Now I would need some help:
Now when images are in page, and image-names are in the js-array, I would need code, which would refresh images after 3 seconds.

I have tried code like this, but it doesn't work:

Code:
<script language="javascript" type="text/javascript">
  var t = 3;
for(i = 0; i < imagenames.length; i++){
  "image"+i+"="+imagenames[i];
}
function Start() {
      tmp = new Date();
      tmp = "?"+tmp.getTime()

for(i = 0; i < imagenames.length; i++){
  document.images["something"].src="image"+i+"="+imagenames[i]+tmp
}


      setTimeout("Start()", t*1000)
      }
      Start();
</script>


I have tried to use this code, which refresh just one image.
This code works:
Code:
<IMG src="image_name.jpg" width="275" height="200" border="1" name="refresh">
      <SCRIPT language="JavaScript" type="text/javascript">
      <!--
      var t = 120 // interval in seconds
      image = "image_name.jpg" //name of the image
      function Start() {
      tmp = new Date();
      tmp = "?"+tmp.getTime()
      document.images["refresh"].src = image+tmp
      setTimeout("Start()", t*1000)
      }
      Start();
      // -->
      </SCRIPT>
 
[tt]<script language="javascript" type="text/javascript">
var t = 3;
[blue]var img_num=0[/blue]
[red]/*[/red]
for(i = 0; i < imagenames.length; i++){
"image"+i+"="+imagenames;
}
[red]*/[/red]
function Start() {
[blue]var[/blue] tmp = new Date();
tmp = "?"+tmp.getTime()
[red]/*[/red]
for(i = 0; i < imagenames.length; i++){
document.images["something"].src="image"+i+"="+imagenames+tmp
}
[red]*/[/red]
[blue]document.images["refresh"].src=imagename
+tmp;[/blue]
[blue]img_num=(img_num+1+imagename.length)%imagename.length;[/blue]
setTimeout("Start()", t*1000)
}
[red]//[/red]Start();
[blue]window.onload=Start;[/blue]
</script>
[/tt]
 
Thanks for your help tsuji !

But. I didn't meant what your code does.

All I want is to refresh images after the 3 sec.

It's like I would push F5 after the 3 sec ( browser refresh the page)

But I don't want to use that technique because user can see the refresh event. I want to use javascript because it is
unnoticed/unobservant to the user.

















 
It would do what I would expect you would have in mind. But, I cannot give you guarantee. With your response, I don't know what to say.
 
Sorry my bad english ;)

When I put images on web-site (by using php), after that I just want refresh images by using javascript.

Image change only if I replace old image with new image which have exactly same name that the previous image.


The problem is, that I have code which refresh just one image which is on the page, but how could I refresh multiply images which are on web site?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top