Scenario: Preloading images
- Perform an ajax query
- Show loading screen
- Retrieve results from ajax query
- Insert images into the dom
- Wait for images to finish loading
- Hide loading screen
I was thinking of doing the following:
function ajaxCallback(results) {
/* snip insert into dom code */
$(".waitForLoad").each(function() {
imageLoadingCount++;
$(this).load(imageLoaded);
});
}
var imageLoadingCount = 0;
function imageLoaded() {
imageLoadingCount--;
if (imageLoadingCount == 0)
HideLoadingScreen();
}
I'm not quite sure of the interaction between the browser DOM and javascript. Does the DOM wait for the javascript to finish executing before it starts loading the images? I'm worried about possible race conditions.