Image preload: Not all images are cached

Discussion in 'Javascript' started by Harod Ruthgar, Jan 22, 2004.

  1. var image = new Image();
    image.src="pathToTheFile";

    The above code is called 25 times (with different pathToTheFile)
    within a loop.

    I manually checked the cache directory and not all images are cached,
    sometimes it's only 3, sometimes 4, etc,
    and mostly only the last few images within the loop.

    However, if I put an alert message in between the lines

    var image = new Image();
    alert("BLAH");
    image.src="pathToTheFile";

    Before I click ok on the alert popup, I check the cache directory, and
    then click the next alert, and so on.
    All images are cached, i.e. everytime I check the cache directory, the
    image that is supposed to be preloaded is cached as expected.

    At first I thought that it might be caused by the delay before I click
    ok on each alert. So I call a 'pause' function to replace the alert,
    here's the pause function code
    function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
    now = new Date();
    if (now.getTime() > exitTime)
    return;
    }
    }
    I have also tried replacing the pause function with window.setTimeout
    function.
    I tried lots of pause/timeout value (in millisecond) and same problem
    occured

    All these were tested on several workstation with IE5, IE6, and NS7.

    Any idea what might cause the problem? And how to fix it?


    Please correct me if my attempt to put a delay before the image.src is
    actually leading me to the wrong direction.

    Thanks in advance.
     
    Harod Ruthgar, Jan 22, 2004
    #1
    1. Advertising

  2. Harod Ruthgar

    Erwin Moller Guest

    Harod Ruthgar wrote:

    > var image = new Image();
    > image.src="pathToTheFile";
    >
    > The above code is called 25 times (with different pathToTheFile)
    > within a loop.
    >
    > I manually checked the cache directory and not all images are cached,
    > sometimes it's only 3, sometimes 4, etc,
    > and mostly only the last few images within the loop.
    >
    > However, if I put an alert message in between the lines
    >
    > var image = new Image();
    > alert("BLAH");
    > image.src="pathToTheFile";


    Hi,

    I think your problem can be fixed easily by using a fresh imageholder every
    time you load, and not put them all in the same var.
    Use an array of Images and assign each image to a new place in the array.

    Good luck.

    Regards,
    Erwin Moller
     
    Erwin Moller, Jan 22, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig G
    Replies:
    0
    Views:
    409
    Craig G
    Mar 7, 2005
  2. Morris

    preload images

    Morris, Nov 12, 2003, in forum: HTML
    Replies:
    5
    Views:
    626
    Beauregard T. Shagnasty
    Nov 14, 2003
  3. Michel
    Replies:
    11
    Views:
    214
  4. hoolie

    Preload images not working

    hoolie, Sep 27, 2004, in forum: Javascript
    Replies:
    5
    Views:
    110
    Thomas 'PointedEars' Lahn
    Oct 11, 2004
  5. cjl
    Replies:
    7
    Views:
    118
Loading...

Share This Page