All image elements are loaded asynchronously by modern browsers.
Check out this simplified image loader script that preloads multiple images and triggers the start() function when all images are fully loaded:
// image loader
// store image paths in the imageURLs array
var imageURLs=[];
imageURLs.push("myImage1.png");
imageURLs.push("myImage2.png");
// the loaded images will be stored in the imgs array
var imgs=[];
var imagesLoaded=0;
loadAllImages(start);
function loadAllImages(callback){
for (var i=0; i<imageURLs.length; i++) {
var img = new Image();
imgs.push(img);
img.onload = function(){
imagesLoaded++;
if (imagesLoaded>=imageURLs.length ) {
callback();
}
};
img.onerror=function(){alert("image load failed");}
img.crossOrigin="anonymous";
img.src = imageURLs[i];
}
}
function start(){
// the imgs array now holds all fully loaded images
// the order of images in imgs matches that of imageURLs
}