javascript - More efficient way to wait for all images to load in jQuery -


i'm using mix of .ready() , .load() execute desired function.

jquery(document).ready(function($) {     $("img").load(function() {         // function goes here     }); }); 

as can see, waits dom ready, on each <img> load, executes code.

if had 1 image load simple.

but problem -- if have 10 images loaded? function called 10 times due each image loading 1 one, , that's not efficient way go achieve want.

so here's question -- there more efficient way wait images load, execute function once?

you avoid having function run multiple times.

jquery(document).ready(function($) {     var nrofimages = $("img").length;     $("img").load(function() {         if(--nrofimages == 0)         {             // function goes here         }     }); }); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -