Carl
Carl

Reputation: 5

How to halt one getjson function until another is finished processing?

I have two getjson functions where one of them is depended on data from the other. How do I halt one getjson until the first one is totally finished loading?

Upvotes: 0

Views: 44

Answers (2)

Jonast92
Jonast92

Reputation: 4967

Use a callback function:

$.getJSON(url, function(json)
{
    // ...    
}).success(function()
{
    // Ready to perform the next operation.
});

You should also note that getJSON is a shorthand Ajax function, which is equivalent to (now using better example):

var request = $.ajax(
{
    type: "POST",
    url: "",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({}),
    dataType: "json",
    async: true
});
request.success(function (msg)
{
    // Make the second request.
}
request.fail(function (jqXHR, textStatus)
{
    // Something went wrong.
});

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Why would you halt one? Simply call the 2nd one inside the first ones callback:

$.getJSON(url, function(json) {
    $.getJSON(url, function(json2) {

    });
});

Upvotes: 1

Related Questions