我以递归方式调用函数,在该函数中,我从服务器请求更新信息。
这是调用函数:
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中止或者我还应该在休息呼叫时使用超时吗?
答案 0 :(得分:1)
clearTimeout
只会阻止再次调用该函数。它不应该阻止在其中采取任何行动。话虽如此,clearTimeout
在您的案例中似乎没有用处。看起来你一个接一个地链接setTimeout
调用,在这种情况下clearTimeout
没用,因为当你输入函数时,超时已经被清除。
在您的情况下,我建议使用setInterval
代替setTimeout
,并调用clearInterval
来中断函数调用。