Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to Automatically reload an image, after it has completed loading... NEED JS HELP!

Posted on 2008-09-29
1
Medium Priority
?
208 Views
Last Modified: 2012-05-05
Hello,

On my website I am trying to continuously refresh an image after it has been loaded but have found this to be quite tricky.  Although it is simple to use JavaScript to refresh the image every 5 seconds, on slow/mobile connections this does not work because it may take more than 5 seconds to load the image, at which point it would start over and try to load it again  thus never completing the update (*Note, It needs to update as fast as possible, so I cannot just increase it to 30 seconds).
So what I really need is some type of notification that the image is done downloading, and then update to the next image.

I imagine this should work like this:
1.      Start Image Load
2.      Wait 500ms, check to see if image has completed loading
                   If it has finished loading, loop back to number 1.
                   If it has not finished loading, loop back to number 2.

I am just not clear on a way to do this in Javascript.  Any ideas on how to make this happen?  Also to note, the main use of this is from the Safari browser, and also I cannot refresh the whole page, I can only refresh the image on the page.

Thanks in advance for any ideas you have on this!
0
Comment
Question by:TheJoeShmoe
1 Comment
 
LVL 3

Accepted Solution

by:
cameroncooke earned 2000 total points
ID: 22599234
Something along these lines should do the job. I use window.setInterval to call a function every 500 milliseconds and if image loaded then I cancel the checking.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <title></title>
 <script type="text/JavaScript">
 
 var intervalId = null;
 
 function startImgLoad() {
   var myImage = document.getElementById('myImage');
   myImage.src = 'filename.jpg';
 
   intervalId = window.setInterval(checkImgLoad, 500);
 }
 
 function checkImgLoad() {
   var myImage = document.getElementById('myImage');
 
   if(myImage.complete) {
     window.clearInterval(intervalId);
     alert('Image loaded!');
   }
 }
 
 </script>
</head>
 
<body onload="startImgLoad();">
 
  <img id="myImage" src="" />
 
</body>
</html>

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question