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
Post a Comment