最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

I'm using a mix of .ready() and .load() to execute my desired function.

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

As you can see, this waits for the DOM to be ready, then on each <img> load, it executes the code.

If I only had one image to load this would be simple.

But the problem is -- what if I have 10 images to be loaded? The function will be called 10 times due to each image loading one by one, and that's not a very efficient way to go about it just to achieve what I want.

So here's the question -- is there a more efficient way to wait for all images to load, then execute the function once?

I'm using a mix of .ready() and .load() to execute my desired function.

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

As you can see, this waits for the DOM to be ready, then on each <img> load, it executes the code.

If I only had one image to load this would be simple.

But the problem is -- what if I have 10 images to be loaded? The function will be called 10 times due to each image loading one by one, and that's not a very efficient way to go about it just to achieve what I want.

So here's the question -- is there a more efficient way to wait for all images to load, then execute the function once?

Share Improve this question asked Aug 13, 2013 at 19:25 Kyle YeoKyle Yeo 2,3686 gold badges31 silver badges53 bronze badges 7
  • 1 Possible duplicate stackoverflow./questions/544993/… – asymptoticFault Commented Aug 13, 2013 at 19:28
  • 1 Dup: Official way to ask jQuery wait for all images to load before executing something (See second answer) – Izkata Commented Aug 13, 2013 at 19:28
  • @asymptoticFault I'm asking to execute once, not the same as stackoverflow./questions/544993/… which is just asking to execute (multiple times) on load. – Kyle Yeo Commented Aug 13, 2013 at 19:29
  • @Izkata Will look at the 2nd answer to see if its what I need, thanks – Kyle Yeo Commented Aug 13, 2013 at 19:32
  • The linked answer does only execute once when all images have loaded. – asymptoticFault Commented Aug 13, 2013 at 19:32
 |  Show 2 more ments

3 Answers 3

Reset to default 4

You could do something like this to avoid having your function run multiple times.

jQuery(document).ready(function($) {
    var nrOfImages = $("img").length;
    $("img").load(function() {
        if(--nrOfImages == 0)
        {
            // Function goes here
        }
    });
});
jQuery(window).load(function() {
    alert("page finished loading now.");
});

jQuery(window).load(...) will be triggered after all content on the page has been loaded. This different from jQuery(document).load(...) which is triggered after the DOM has been loaded. I think this will solve your issue.

If anybody wants to know, my final result was this:

(function($) {
    $(window).load(function(){
        // Function goes here
    });
})(jQuery);

that's because

jQuery(window).load(function($) {});

isn't a jQuery object, as referenced in this question:

Calling jQuery on (window).load and passing variable for 'No Conflict' code

发布评论

评论列表(0)

  1. 暂无评论