如何使用setTimeout每隔10毫秒检查xmlhttp.readyState

How I Can Check xmlhttp.readyState with setTimeout every 10 milliseconds?

本文关键字:检查 xmlhttp readyState 10毫 何使用 setTimeout 每隔      更新时间:2023-09-26

嗨,我是java脚本的新手,如何使用xmlhttp.onreadystatechange=function()每10毫秒检查一次函数的状态,xmlhttp.readyState,使用setTimeout,我不知道如何每10毫秒进行一次。

谢谢。

xmlhttp.onreadystatechange无论何时更改状态,都将被调用

您不需要setTimeout来检查xmlhttp.readyState的值。

xmlhttp.onreadystatechange = function () {
    console.log("readyState is now: " + xmlhttp.readyState);
}

可以执行以下操作:

var state = 0;
xmlhttp.onreadystatechange = function () {
    state = xmlhttp.readyState;
}
setInterval(function () {
    console.log("readyState is now: " + state );
}, 100); // prints the readystate every 100ms

但我不明白你为什么要这样做。

此函数将每10ms持续重复一次:

(function readyStateCheck(){
        //code to do the ready-state check
        if  (!someBreakingCondition){
            setTimeout(readyStateCheck, 10);
        }
})()