I have an image and I want to automatically update it using either javascript, jquery or ajax.
Until now I have the following code.
<html>
<head>
<script language="JavaScript"><!--
function refreshIt() {
if (!document.images) return;
document.images['myCam'].src = 'swt.png';
setTimeout('refreshIt()',50); // refresh every 50ms
}
//--></script>
</head>
<body onLoad=" setTimeout('refreshIt()',50)">
<img src="swt.png" name="myCam">
</body>
</html>
I think it is not working because the browser is caching the image and it is not updating the image correctly.
Can someone provide me a working example?
On top of loading a different image each time and increasing the refresh time, you should start the
setTimeout
again after the image has fully loaded.Just add a random number to the query string on every refresh. This will trick the browser into thinking this is a different resource, while the server will serve the same picture.
You are setting the image source to the same string. That will not refresh the image, the browser thinks it already has that image, and won't issue another request. Try adding a random query parameter each time you want to refresh - then the browser should see the image as different resources.
This will do the trick, but it's a nightmare as far as performance.
Replace this line:
with:
This will generate different link each time and cached version of the img won't be used.
And don't refresh the image every 50ms. That is way too often.
The solutions in this thread will work, but they force the browser to download the image every time the function runs. This will check for a new image without actually downloading until a new one is available. This process should be split off into a Worker for optimal performance.