Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if all images are loaded

Here is my attempt at the ability to test if all images are loaded:

for (var i = 0; i < imgCount; i ++) {
    loadArr[i] = false
    imgArr[i] = new Image()
    imgArr[i].src='img'+i+'.png'
    imgArr[i].onload = function() {
        loadArr[i] = true //but wait! At the end of
                          //the loop, i is imgCount
                          //so this doesn't work.
    }
}

The problem is that once the loop is done, the variable i is imgCount. That means all the other "loaded" flags never get set to true when their images are loaded.

Is there some way to add a "loaded" property to an image, or is there some workaround to this problem?

like image 257
tckmn Avatar asked Mar 23 '13 13:03

tckmn


People also ask

How do you know if all images are loaded react?

The <img /> element has the onLoad event that allows us to detect when the image is loaded, and also the onError event that tells us if an error happens when trying to retrieve the image.

How can we detect an image been fully loaded in the browser?

The image is considered completely loaded if any of the following are true: Neither the src nor the srcset attribute is specified. The srcset attribute is absent and the src attribute, while specified, is the empty string ( "" ). The image resource has been fully fetched and has been queued for rendering/compositing.

How check page is loaded or not in Javascript?

If you need the whole page to be ready, including images, check for document. readyState === "complete" . @costa, if document. readyState == "complete" , it means everything, including images, have been loaded.


2 Answers

You need to define the onload function using a closure:

for (var i = 0; i < imgCount; i ++) {
    loadArr[i] = false
    imgArr[i] = new Image()
    imgArr[i].src='img'+i+'.png'
    imgArr[i].onload = (function(i){
        return function(){ loadArr[i] = true }
    })(i);
}

Here's a jsFiddle which demonstrates this working in a similar scenario.

Also, note that the solution you've currently selected as the answer doesn't actually work:

imgArr[i].onload = (function() {
        loadArr[i] = true;
    })();

This function is evaluated immediately. This means that in the loop, each element of the loadArr is set to true as is the onload event. That code is functionally identical to:

imgArr[i].onload = loadArr[i] = true;
like image 121
ic3b3rg Avatar answered Oct 23 '22 15:10

ic3b3rg


You must pass the index value to anonymous function like this,

for (var i = 0; i < imgCount; i++) {
    loadArr[i] = false
    imgArr[i] = new Image()
    imgArr[i].src = 'img' + i + '.png'
    imgArr[i].onload = function (index) {
        return function () {
            loadArr[index] = true //but wait! At the end of
            //the loop, i is imgCount
            //so this doesn't work.
        };
    }(i);
}
like image 28
Okan Kocyigit Avatar answered Oct 23 '22 14:10

Okan Kocyigit