Grozav Alex Ioan
Grozav Alex Ioan

Reputation: 1559

Loading elements in a specific order using ajax/jquery

When someone enters my page, I want to load #portfolio and #header divs first on the page.

Then I want to start loading #slide after the first 2 have finished loading (to improve functionality).

How can I do that using ajax/jquery?

Thanks a lot in advance!

Upvotes: 3

Views: 1705

Answers (2)

lonesomeday
lonesomeday

Reputation: 237865

I presume you are using calls to $.ajax to do the loading of #portfolio and #header.

From jQuery 1.5, you can use $.when to perform an action when multiple AJAX requests have completed:

$.when(
    $.ajax({
        /* options to load #portfolio */
    }),
    $.ajax({
        /* options to load #header */
    })
).then(function() {
    $.ajax({
       /* options to load #slide */
    });
});

Upvotes: 7

Saeed Neamati
Saeed Neamati

Reputation: 35832

You can nest ajax calls and get each next step on the success ajax call of the previous step.

$.ajax({ // Getting portfolio hear
     success: function(){
        $.ajax({ // Getting header here, on success callback of portfolio call
            success: function(){
                $.ajax({ // Getting slide here, on success callback of header
                    // So on,
                });
             }
        });
     }
});

Upvotes: 1

Related Questions