我怎么能每10毫秒用setTimeout检查xmlhttp.readyState?

时间:2013-11-19 12:44:03

标签: javascript html ajax

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

谢谢。

2 个答案:

答案 0 :(得分:1)

无论何时改变状态,

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

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

答案 1 :(得分:0)

这将会每隔10毫秒不断递归一次:

(function readyStateCheck(){

        //code to do the ready-state check

        if  (!someBreakingCondition){

            setTimeout(readyStateCheck, 10);

        }

})()