Solved

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

Posted on 2008-09-29
1
197 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

19 Experts available now in Live!

Get 1:1 Help Now