Solved

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

Posted on 2008-09-29
1
201 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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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 tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

777 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