无法阅读Javascript代码

Too*_*ool 2 javascript xmlhttprequest

我是JS的新手,并且很难阅读以下JS代码.

该函数的第一个参数是PHP脚本的url,第二个是字符串.

令我困惑的是如何在行后读取代码:self.xmlHttpReq.open('POST',strURL,true);

这之后会发生什么?我应该看看这行代码?剧本?打开后会发生什么?

function check_detail(strURL, pids) 
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() 
    {
        if (self.xmlHttpReq.readyState == 4)
            updatepage(self.xmlHttpReq.responseText, pids);
    }
    self.xmlHttpReq.send(getquery(pids));
}
Run Code Online (Sandbox Code Playgroud)

Poi*_*nty 7

关键是调用"send()",它实际上启动了HTTP请求.发生这种情况,但代码然后立即进行而不等待结果.

当服务器响应时,浏览器将调用设置为"readystatechange"处理程序的匿名函数.究竟何时发生这种情况是不可预测的; 换句话说,它是异步的.

因此,"updatepage()"调用将在"check_detail()"函数返回后很久发生.