clearTimeout是否会中止异步XMLHttpRequest?

时间:2014-04-16 13:33:36

标签: javascript timer xmlhttprequest

我以递归方式调用函数,在该函数中,我从服务器请求更新信息。

这是调用函数:

    function StartFeedUp() {
        GetNextImage();
        if (tmrStartFeedUp) window.clearTimeout(tmrStartFeedUp);
        tmrStartFeedUp = setTimeout(StartFeedUp, 100);
    }

这是被调用的函数:

            var urlTS = '/Cloud/isNewFrames.ashx?alias=' + alias + '&version=' + version + '&guidlogon=' + guidlogon;
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.open("POST", urlTS, true);
            xmlhttp.send();
            var nextts = xmlhttp.responseText;
            //do something with info

当达到100ms的时间限制时,XMLHttpRequest中止或者我还应该在休息呼叫时使用超时吗?

1 个答案:

答案 0 :(得分:1)

clearTimeout只会阻止再次调用该函数。它不应该阻止在其中采取任何行动。话虽如此,clearTimeout在您的案例中似乎没有用处。看起来你一个接一个地链接setTimeout调用,在这种情况下clearTimeout没用,因为当你输入函数时,超时已经被清除。

在您的情况下,我建议使用setInterval代替setTimeout,并调用clearInterval来中断函数调用。