AJAX - 帮助我理解代码序列
AJAX - help me understand that sequence of code
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
有人可以帮我理解这里的执行顺序吗?
xhttp.onreadystatechange 被调用,IF 等待状态 ==4。但直到 xhttp.send() 在代码中进一步触发才为 4。那么一旦xhttp.send()触发了State=4,为什么会再次调用onreadystatechange执行IF呢?我想我将其视为自上而下的执行。我只是没有得到 how/why onreadystatechange "waits" 状态改变?因为它的代码已经被执行了。请尽可能简单的解释,谢谢。
onreadystatechange is an event handler,这意味着它会在特定事件触发时触发。
The XMLHttpRequest.onreadystatechange property contains the event
handler to be called when the readystatechange event is fired, that is
every time the readyState property of the XMLHttpRequest changes
(强调我的)
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
有人可以帮我理解这里的执行顺序吗?
xhttp.onreadystatechange 被调用,IF 等待状态 ==4。但直到 xhttp.send() 在代码中进一步触发才为 4。那么一旦xhttp.send()触发了State=4,为什么会再次调用onreadystatechange执行IF呢?我想我将其视为自上而下的执行。我只是没有得到 how/why onreadystatechange "waits" 状态改变?因为它的代码已经被执行了。请尽可能简单的解释,谢谢。
onreadystatechange is an event handler,这意味着它会在特定事件触发时触发。
The XMLHttpRequest.onreadystatechange property contains the event handler to be called when the readystatechange event is fired, that is every time the readyState property of the XMLHttpRequest changes
(强调我的)