最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wait for Json call to be completed in javascript - Stack Overflow

programmeradmin7浏览0评论

I am using the below json call in my javascript method

function go123(){
    var cityName = "";
    var temp = $.getJSON("=?", args,function (data) {
        if (data.properties.city != null){ 
            cityName = data.properties.city;
            check = true;
        } else {
            cityName = "NaN"
        }
    }); // end of my Json Call.

    // my validation is done below
    if(cityName != "NaN"){
        return false;
    } else {
    // here I except the cityName to not be "" but be some value which  is set as :cityName = data.properties.city;
        return true;
    }
} // end of my function 

Now what problem I am facing is that before my Json call is pelete the next set of statements ( in the code below the line "// my validation is done below " ) is already executed.

I want to get the values set in my json call (cityName) and only once when the call is pleted then only I want the next set of statements to be executed.

Please help me on this. Any advice/ideas/suggestions will be highly appreciated ! Thanks.

I am using the below json call in my javascript method

function go123(){
    var cityName = "";
    var temp = $.getJSON("https://abc.in/api/city?callback=?", args,function (data) {
        if (data.properties.city != null){ 
            cityName = data.properties.city;
            check = true;
        } else {
            cityName = "NaN"
        }
    }); // end of my Json Call.

    // my validation is done below
    if(cityName != "NaN"){
        return false;
    } else {
    // here I except the cityName to not be "" but be some value which  is set as :cityName = data.properties.city;
        return true;
    }
} // end of my function 

Now what problem I am facing is that before my Json call is pelete the next set of statements ( in the code below the line "// my validation is done below " ) is already executed.

I want to get the values set in my json call (cityName) and only once when the call is pleted then only I want the next set of statements to be executed.

Please help me on this. Any advice/ideas/suggestions will be highly appreciated ! Thanks.

Share Improve this question edited Apr 28, 2012 at 8:00 Joseph 120k30 gold badges184 silver badges237 bronze badges asked Apr 28, 2012 at 7:56 Yasser ShaikhYasser Shaikh 47.8k50 gold badges208 silver badges287 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

The function you passed into $.getJSON() is the callback run when the function pletes successfully. All else being equal, stick the "rest of it" inside that method. If you can't do so, what you're after is called a jQuery Deferred. See http://www.erichynds./jquery/using-deferreds-in-jquery/ and http://joseoncode./2011/09/26/a-walkthrough-jquery-deferred-and-promise/ for code that looks like so:

var req = $.getJSON('blah', 'de', 'blah');

req.success(function(response){
    // The request is done, and we can do something else
});

AJAX calls are asyncrhonous. They don't wait for the reply. They operate in the background and execute the code that follows it immediately after the call. Therefore, the data received in getJSON is not yet there when the operations below it are executed.

You can put the operations you want in the callback so that they get executed when the data is revceived:

function go123(callback){
    var temp = $.getJSON("https://abc.in/api/city?callback=?", args,function (data) {
        //execute the callback, passing it the data
        callback(data);
    });
}

//when you call go123, it get's back the result:
function goBefore123(){

    //get our JSON
    go123(function(data){

        //when we get our data, evaluate
        if (data.properties.city != null){

            cityName = data.properties.city;
            check = true;

            alert('executed after returned');
            afterCall();
        } else {
            cityName = "NaN"
        }
    });

    alert('i am executed before anything else');
}

function afterCall(){
    alert('im also executed after');
}

Calling an external url will take too much time, wait for the result Check below

var jqxhr = $.getJSON("example.json", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.plete(function() { alert("plete"); });

http://api.jquery./jQuery.getJSON/

.success

http://api.jquery./jQuery.getJSON/

发布评论

评论列表(0)

  1. 暂无评论