0

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) Commented Aug 3, 2010 at 4:20
  • I meant I'm going to need something from the server. Commented Aug 3, 2010 at 4:25

5 Answers 5

1

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" />
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. But what if the images should come from the server and not initially available?
That depends on how the server can provide the image. Is it from database or something?
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?
1

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?

Comments

0

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

Comments

0

You can use

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.