Uploading and adding images gradually

I have a problem that I cannot handle. I created a simple jQuery album gallery and I have this function:

function loadAlbum (index) {
    for (var j=1; j < xmlData[index].length; j++) {

        var img = new Image();

        $(img).load(function() {
                    $(this).hide();
                    $('#album'+index+' .photoContainer').append(this);
                    $(this).fadeIn();
                })
                .error(function(){
                    //alert("Could not load one or more photo!");
                })
                .attr({
                    'src': xmlData[index][j],
                    'id': 'img'+index+j,
                    'class': 'photoFrame',
                    'width': newW,
                    'height': newH
                })
                .css({
                    'width': newW,
                    'height': newH
                });
    };
};

Now, since you can see that all src images are imported from an external XML file containing data (image names are progressive ex.:photo001.jpg, photo002.jpg, etc.).

They are created in the DOM through a for loop and added to the div.

What problem can you say? I need all the images to be added in a progressive form, as indicated in the XML data, but this only happens on the local computer, and not when uploading to any server. I believe this is due to the different loading times of each image depending on the size ... but I still can’t understand how it works. Somebody knows?

+3
1

. .

function loadAlbum(index) {
    var i = 0;
    var j = xmlData[index].length;
    function loadImage() {
         if (i >= j) return;

         // do what you're doing in the loop except for ....
         $(img).load(function() {
             $(this).hide();      
             $('#album'+index+' .photoContainer').append(this);
             $(this).fadeIn();
             // increments i by 1 and calls loadImage for the next image.
             i++;
             loadImage();
         // etc.
    }
    loadImage();
}

, , :)

+7

Source: https://habr.com/ru/post/1796724/


All Articles