Image preload: Not all images are cached

H

Harod Ruthgar

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.
 
E

Erwin Moller

Harod said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top