Re: javascript question

by "Cyanide _7" <leo7278(at)hotmail.com>

 Date:  Thu, 04 May 2000 03:37:52 CDT
 To:  pbabcock(at)bgsgroup.com
 Cc:  hwg-languages(at)hwg.org
  todo: View Thread, Original
>HI all
>
>I have a javascript function that preloads a bunch of images.  I have
>another function that I want to execute only when all of these images are
>fully downloaded.  How can I do this?
>
>This is what I have:
>
>function preload(){
>
>image1 = new image();
>image1.src = "test.gif";
>//etc for a  whole lot of images
>
>do_this_function_after_preload_has_completed();
>
>return true;
>}
>
>However the second function is triggered right away, not after the images
>are all loaded.  Does it not process the function one line at a time,
>loading one image then the next then the next and finally doing the
>"do_this_function_after_preload_has_completed();" function?

keep in mind that aImage.src = "myImage.jpg"; is merely an assignment 
statement. it is executed and the following code is completed. the src 
property is infact set at that moment, but the downloading of the file 
happens behind the scene. the Image object has a complete property and you 
can poll those values until all are true and then calling the function. this 
requires an intermediate function and timeouts:


var images = new Array();
function preload(){

  images[0] = new image();
  images[0].src = "test1.gif";
  images[1] = new image();
  images[1].src = "test2.gif";

  intermediate();
  return true;
}

function intermediate(){
  var completed = true, i = 0;
  while(i<images.length && completed){
    if(!images[i].complete)
      completed = false;
    else
      i++;
  }
  if(completed)
    do_this_function_after_preload_has_completed();
  else
    setTimeout("intermediate();",0);
}

that should do it. of course i'm assuming this preload function is being 
called after the body has loaded? if these images are being created and set 
*inline* with the page loading, then the onLoad event handler is not fired 
until the images are all loaded (someone mentioned that already). this is 
just one way to do this without that event handler. good luck - Cyanide_7

PS: sorry if there are any syntax errors, i wrote it on the fly.


>
>hope that makes sense
>
>Phil Babcock
>Lead Web Designer
>BGS Group Inc.
>

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA