【发布时间】:2013-04-24 06:43:06
【问题描述】:
我有一些 AJAX 使用 setInterval() 显示进度条以获取脚本的当前进度。我的问题是,当进度达到 100% 时,我似乎无法杀死它。我不确定这是否与范围有关,但我的处理程序是全局的,所以我不知道为什么它不起作用。这是我所拥有的:
function showLog(){
document.getElementById('log').style.display = "block";
clearInterval(inth);
return false;
}
function startAjax(){
var inth = setInterval(function(){
if (window.XMLHttpRequest){ xmlhttpp=new XMLHttpRequest();}else{ xmlhttpp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttpp.onreadystatechange=function(){
if(xmlhttpp.readyState==4 && xmlhttpp.status==200){
document.getElementById("sbar").innerHTML=xmlhttpp.responseText;
}
}
xmlhttpp.open("POST","scrape.php",true);
xmlhttpp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var sitelist = document.getElementById('website').value;
var par = "website="+sitelist;
xmlhttpp.send(par);
}, 5000);
return false;
}
为什么 clearInterval 不起作用?我做错了什么?
【问题讨论】:
-
决定何时达到 100% 的代码在哪里?
-
@Jack 一旦进度达到 100%,它就会打印在
scrape.php。最初我尝试了if(xmlhttpp.responseText == "100%")类型的东西,但效果并不好。
标签: javascript ajax scope setinterval clearinterval