异步回调

Asynchronous callback

本文关键字:回调 异步      更新时间:2023-09-26

我阅读了js中的异步函数并找到了如何处理它们,但我仍然不明白为什么在表达式不匹配时调用第一个警报。

xhr.onreadystatechange = function(){
    // Ready state 4 means the request is done
    if(xhr.readyState === 4) {
        // 200 is a successful return
        if(xhr.status === 200) {
            alert(xhr.responseText);
        } else {
            alert('Error: '+xhr.status);
        }
    }
}

尝试调试代码,在if(xhr.status === 200)行上使用断点,并确保该值未200