我有一个每隔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();
,如果我时间只运行第二个代码时的时间间隔蜱,并执行第一个呼叫,呼叫实际上是在大约同一时间运行。但是,似乎间隔时间不知何故死亡 - 他的鸽子不再勾选。
这是一个已知的问题还是我只是没有看到大的东西......
感谢您的帮助!
您将全部**变量声明为隐式全局变量。使用'var'语句对''intervalTick()'函数执行本地'xmlhttp'请求。这不会解决您的问题,但它不会帮助它。 – Matt
此外,你应该***永远不会传递字符串到'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
哦, 'setTimeout'和'setInterval'的超时值应该是一个整数,而不是一个字符串,所以使用'window.setInterval(intervalTick,3000);' – Matt