Lolo
Lolo

Reputation: 537

Stop script while a file is not loaded

I have this code and I'd like to stop the script while $.get() is not finished.

$.get("assets/data/html/navigation.html", function(response) {
    $('body').append(response);
});
$('body').append('<div class="modal">...</div>');

In this example the second append is executed before the first one.

Thanx for your help

Upvotes: 0

Views: 50

Answers (2)

bokorben
bokorben

Reputation: 318

According to jQuery documentation:

    var jqxhr = $.get("assets/data/html/navigation.html", function(response) {
      $('body').append(response);
    })
    .done(function() {
      $('body').append('<div class="modal">...</div>');
    })

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382132

No, you don't want to stop the script, you want to go on from inside the callback :

$.get("assets/data/html/navigation.html", function(response) {
    $('body').append(response);
    doOtherStuff();
});
function doOtherStuff(){
    $('body').append('<div class="modal">...</div>');
}

Or, using done :

$.get("assets/data/html/navigation.html", function(response) {
    $('body').append(response);
}).done(function(){
    $('body').append('<div class="modal">...</div>');
});

Upvotes: 3

Related Questions