What @variant said + code, since the img onload event does not always fire:
var img = new Image(), div = $( "<div>" ).appendTo( "#image-container").hide(); img.onload = function(){ if( this.isLoaded ) { return; } this.isLoaded = true; loadedImages.push( div.css( "background-image", "url('"+this.src+"') no-repeat center center" ) ); } img.src = images[imageToLoad].url; if( ( img.complete || img.readyState === 4 ) && !img.isLoaded ) { img.onload(); }
source share