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
2010-08-03 04:02:52
+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
2010-08-03 04:07:37
Thanks. But what if the images should come from the server and not initially available?
Frodoro
2010-08-03 04:28:17
That depends on how the server can provide the image. Is it from database or something?
Reigel
2010-08-03 04:41:56
Yep, from the database.
Frodoro
2010-08-03 05:11:30
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
2010-08-03 05:30:49
+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
2010-08-03 06:08:14
A:
Use jquery plugin http://www.protofunc.com/scripts/jquery/ajaxManager3/ This plugin is compatible with jQuery current version 1.4.x.
enam
2010-08-03 09:30:45