Skip to content
Advertisement

jQuery: Possible to wait for $.get to finish loading before continuing?

The folowing script does not wait for $.get to finish loading the page before continuing with the loop:

$.each(data.songs, function(index, val) {
    $('#nowartist')
         .append('song starting');
    $.get("http://localhost/play.php", function(data){
         alert('done');
    });
});

data is a JSON object

Any ideas or comments will be greatly appreciated.

Advertisement

Answer

$.ajax({
     async: false,
     type: 'GET',
     url: 'http://localhost/play.php',
     success: function(data) {
          //callback
     }
});

That should do it.

Old docs

2021 docs

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement