views:

98

answers:

5

Let's say initially I have an image loaded in my view. Every now and then, I want that image to change via ajax with an interval of 10 sec. How can I do that? Thanks.

A: 

You can use the setTimeout command in javascript to do a jQuery postback and bring back the new image and render that to your control

griegs
+1  A: 

Through setInterval().

Something like this,

var newimage = ['../image0.gif','../image1.gif','../image2.gif',
                '../image3.gif','../image4.gif']; 
setInterval(function(){
  $('#imgID').attr('src',newimage[Math.floor(Math.random()*newimage.length)]);
  // this will give you random images, but you can also not random it..
},10000);

html like this,

<img src="image0.gif" alt="image" id="imgID" />
Reigel
Thanks. But what if the images should come from the server and not initially available?
Frodoro
That depends on how the server can provide the image. Is it from database or something?
Reigel
Yep, from the database.
Frodoro
hehe I don't really work on server-side. How would it be? is it like `http://somesite.com/image.php?img=432423`, that would return an image?
Reigel
+1  A: 

use reigel's code and from the server return simple the image path. then load that path as the attribute 'src' of the image using jquery or anyway other way you know. need more clarity?

Anand
A: 

Hello,

You can use

http://plugins.jquery.com/project/AjaxManager

JQuery Guru
A: 

Use jquery plugin http://www.protofunc.com/scripts/jquery/ajaxManager3/ This plugin is compatible with jQuery current version 1.4.x.

enam