2012-06-01 34 views
0

我有一个每隔3秒运行一个函数的间隔。间隔被两个Ajax调用阻塞

intervalStepper = window.setInterval('intervalTick()','3000'); 

function intervalTick() { 
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else {// code for IE6, IE5 
     xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      gotResult(xmlhttp.responseText); 
     } 
    } 
    xmlhttp.open('GET','index.php?ajax=true',true); 
    xmlhttp.send(); 
} 
function gotResult(res) { 
    alert(res); 
} 

此外,我只是另一个Ajax调用,它运行在按钮单击。现在

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else {// code for IE6, IE5 
    xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
} 
xmlhttp.onreadystatechange=function() { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
     agentDetailGotten(xmlhttp.responseText); 
    } 
} 
xmlhttp.open('GET','index.php?anotherPage=true',true); 
xmlhttp.send(); 

,如果我时间只运行第二个代码时的时间间隔蜱,并执行第一个呼叫,呼叫实际上是在大约同一时间运行。但是,似乎间隔时间不知何故死亡 - 他的鸽子不再勾选。

这是一个已知的问题还是我只是没有看到大的东西......

感谢您的帮助!

+3

您将全部**变量声明为隐式全局变量。使用'var'语句对''intervalTick()'函数执行本地'xmlhttp'请求。这不会解决您的问题,但它不会帮助它。 – Matt

+2

此外,你应该***永远不会传递字符串到'setTimeout' /'setInterval':使用'window.setInterval(intervalTick,'3000');'代替。欲了解更多信息,请参阅[这里](http://stackoverflow.com/questions/6232574/is-it-bad-practice-to-pass-a-string-to-settimeout-if-yes-why) – Matt

+1

哦, 'setTimeout'和'setInterval'的超时值应该是一个整数,而不是一个字符串,所以使用'window.setInterval(intervalTick,3000);' – Matt

回答

0

我刚才解决它。

看来这有点像一个Firefox的错误(在bugzilla.mozilla.org找到)

NS_ERROR_NOT_AVAILABLE

这一次并没有显示出我,但我刚才发现。它会在Firefox尝试同时执行两个调用时出现。

欲了解更多信息,我发现了一个博客条目here

我解决了这个问题,如果一个呼叫正在运行,另外一个等待的时间。

0

尝试清除并重新设置您的间隔时间:

intervalStepper = window.setInterval('intervalTick()',3000); 

function intervalTick() { 

    window.clearInterval(intervalStepper); 

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else {// code for IE6, IE5 
     xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
      gotResult(xmlhttp.responseText); 
     } 
    } 
    xmlhttp.open('GET','index.php?ajax=true',true); 
    xmlhttp.send(); 

    intervalStepper = window.setInterval('intervalTick()',3000); 

} 

function gotResult(res) { 
    alert(res); 
}