0

假设我们有一个从数据库获取一些数据的函数:

function getResults() {
    if (httpReq.readyState == 4 || httpReq.readyState == 0) {
        httpReq.open("GET",'../search.php?blablabla',true);
        httpReq.onreadystatechange = function() {
            if (httpReq.readyState == 4) {
                // Some code here
            }
        };
        httpReq.send(null);
    }
    updateResults();
    // This function is running before the code above
    // ...so I actually get no results
}

如何updateResults()在已经从数据库中获取结果的那一刻运行该函数?

4

1 回答 1

1

我认为代码应该是这样的:

function getResults() {
    httpReq.open("GET",'../search.php?blablabla',true);
    httpReq.onreadystatechange = function() {
        if (httpReq.readyState == 4) {
            // Some code here
            updateResults();
        }
    };
    httpReq.send(null);
}
于 2012-11-09T17:23:10.867 回答