0
function updateimage(){
    $("#fileimg").attr("src","path/to/image.jpg");
    $('#fileimg').fadeIn('slow');
    setTimeout(updateimage, 5000);
}

Hey,

I want to reload an image every 5 seconds but that doesn't work, it stays the same, but when you F5 the page it do refreshes. How can i refresh it every 5 sec normally that the image updates too?

2 Answers 2

6

It's cached, so it doesn't need to reload. If you want to force a fresh load you need to make the browser think it's a different file:

function updateimage(){ 
    $("#fileimg").attr("src","path/to/image.jpg?" + new Date());  
    $('#fileimg').fadeIn('slow');  
    setTimeout(updateimage, 5000); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Is there a solution that respects http caching? If the image only changes sometimes, it would be great not to force every client to do a complete reload.

E.g. I have a webcam pic that updates every 5 seconds. Even if each client does a ?dateMillis reload every 5 seconds, intermediate caches will still not correctly reuse the data between clients. Allowing standard cache timeouts (or even etag checks) to work would save a lot of resources.

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.