Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now