javascript - Fire an event after preloading images -


this code use preload images, i'm not sure if it's best one. question is, how can fire , event, example alert(); dialog after has finished loading images?

var preload = ["a.gif", "b.gif", "c.gif"];     var images = [];     (i = 0; < preload.length; i++) {         images[i] = new image();         images[i].src = preload[i];     } 

you can use new "$.deferred" if like:

var preload = ["a.gif", "b.gif", "c.gif"]; var promises = []; (var = 0; < preload.length; i++) {     (function(url, promise) {         var img = new image();         img.onload = function() {           promise.resolve();         };         img.src = url;     })(preload[i], promises[i] = $.deferred()); } $.when.apply($, promises).done(function() {   alert("all images ready sir!"); }); 

might little risky leave image objects floating around, if fixed shifting closure. edit in fact i'll change myself because it's bugging me :-)


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -