0

我有一些显示进度条的 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 不起作用?我究竟做错了什么?

4

1 回答 1

2

这是一个范围问题,var inth将函数外部声明为全局变量。并inth = setInterval(...)startAjax函数中使用。

正如您在问题中所说,您的处理程序是全球性的。但变量本身不是,所以不能在函数作用域外访问。

于 2013-04-24T06:44:32.417 回答