PDA

View Full Version : question about function for preloading images



tetrix
08-27-2008, 08:24 AM
hi,

would this function work for preloading images?


function Load(){
var photoNames = ["over0","over1","over2","over3","over4","over5","over6","over7","over8","over9","over10",
"over11","over12","over13","over14","over15","over16","over2","over17","over18","over19","over20","over21",
"over22","over23"];
var photoObjects = [];
for (var i = 0; i < photoNames.length; i++)
{
photoObjects[i] = new Image();
photoObjects[i].src = 'images/collection/' + photoNames[i] + '.jpg';
}
}

then i would put it as a onload event in the body tag(onload='Load()'), is the function correct?

codeexploiter
08-27-2008, 08:46 AM
The code mentioned seems to be correct and should work if the image path and names are correct (make sure you have those images in the mentioned path).

clueful
08-28-2008, 04:51 PM
hi,

would this function work for preloading images?

then i would put it as a onload event in the body tag(onload='Load()'), is the function correct?



var photoObjects = [];
}

No because you are storing the images in a volatile array that is destroyed as soon as the function terminates, long before the images have time to download. You can use the this keyword to make the array persistent:

function Load(){
var photoNames = ["over0","over1","over2","over3","over4","over5","over6","over7","over8","over9","over10",
"over11","over12","over13","over14","over15","over16","over2","over17","over18","over19","over20","over21",
"over22","over23"];

this.photoObjects = [];

for (var i = 0; i < photoNames.length; i++)
{
this.photoObjects[i] = new Image();
this.photoObjects[i].src = 'images/collection/' + photoNames[i] + '.jpg';
}
}

magicyte
08-29-2008, 02:32 AM
Agreed. This would preload images. As for the actual display of image speed depends on your internet service.

-magicyte