开发者

I don't understand how this javascript function executes

When I run the following codes, the alert popup displays undefined. I thought it would return either true or false instead. Please can someone explain how the checkLoginStatus() function excutes. Thanks.

function checkLoginStatus() {
$.get("func.php", {op:'login_status', r:Math.random()}, function(data) {
    if (data == "Yes") {
        showSalesView();
        return true;
    } else {
        loginView();
        retur开发者_如何学运维n false;
    }   
});

}

alert(checkLoginStatus());


There's a couple things wrong.

One, you're performing an asynchronous call within the function, so by the time the call has come back, checkLoginStatus has already returned. It essentially looks like this:

function checkLoginStatus() {
    $.get("func.php", {
        op: 'login_status',
        r: Math.random()
    }, function(data) {
        if (data == "Yes") {
            showSalesView();
            return true;
        } else {
            loginView();
            return false;
        }
    });
    // return undefined
}

Second, you're returning within the callback of another function, so that return affects the return value of the callback to $.get

You want to use callbacks. So,

function checkLoginStatus(callback) {
    $.get("func.php", {
        op: 'login_status',
        r: Math.random()
    }, function(data) {
        if (data == "Yes") {
            showSalesView();
            callback(true);
        } else {
            loginView();
            callback(false);
        }
    });
}

and then

checkLoginStatus(function(result) {
    alert(result);
});


The AJAX call is asynchronous, so the function that you specify as callback will be executed when the response arrives. The code doesn't wait for the response to exit from the checkLoginStatus function.

You can use a callback method to display the result:

function checkLoginStatus(callback) {
  $.get("func.php", {op:'login_status', r:Math.random()}, function(data) {
    if (data == "Yes") {
      showSalesView();
      callback(true);
    } else {
      loginView();
      callback(false);
    }   
  });
}

checkLoginStatus(function(status){ alert(status); });


what you are seeing is the undefined (void) return from the .get() function, notice carefully that .get function call contains another function as the third parameter ( first being the url, second being an anon object) that is the "callback" for the results of the .get function, this is called later when the results have been returned from the server.


The returned bool is being returned by the $.get functions callback, not checkLoginStatus.

function checkLoginStatus() {
    $.get("func.php", {op:'login_status', r:Math.random()}, 

        // Start Callback Function
        function(data) {
            if (data == "Yes") {
                showSalesView();
                return true;
            } else {
                loginView();
                return false;
            } 
        // End Callback Function

    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜