?
Solved

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

Posted on 2008-09-29
1
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
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…
Suggested Courses

718 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