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.
2
-
When you use the term "ajax" do you necessarily mean you want a call to the server or just dynamic HTML from a client side list? (probably depends on how many images you are cycling/choosing between)Brenton Alker– Brenton Alker2010-08-03 04:20:05 +00:00Commented Aug 3, 2010 at 4:20
-
I meant I'm going to need something from the server.Frodoro– Frodoro2010-08-03 04:25:22 +00:00Commented Aug 3, 2010 at 4:25
Add a comment
|
5 Answers
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" />
3 Comments
Frodoro
Thanks. But what if the images should come from the server and not initially available?
Reigel Gallarde
That depends on how the server can provide the image. Is it from database or something?
Reigel Gallarde
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?Use jquery plugin http://www.protofunc.com/scripts/jquery/ajaxManager3/ This plugin is compatible with jQuery current version 1.4.x.