Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-清除interval和setinterval范围_Javascript_Ajax_Scope_Setinterval_Clearinterval - Fatal编程技术网

Javascript-清除interval和setinterval范围

Javascript-清除interval和setinterval范围,javascript,ajax,scope,setinterval,clearinterval,Javascript,Ajax,Scope,Setinterval,Clearinterval,我有一些AJAX,它使用setInterval()显示进度条,以获取脚本的当前进度。我的问题是,当进步达到100%时,我似乎无法扼杀它。我不确定这是否与作用域有关,但我的处理程序是全局的,所以我不知道为什么它不起作用。以下是我所拥有的: function showLog(){ document.getElementById('log').style.display = "block"; clearInterval(inth); return false; } funct

我有一些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不工作?我做错了什么?

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


正如您在问题中所说,您的处理程序是全局的。但是变量本身不是,因此不能在函数范围之外访问它。

决定何时达到100%的代码在哪里?@Jack一旦进度达到100%,它将在
scrap.php
中打印出来。最初我尝试了一种
if(xmlhttpp.responseText==“100%”)类型的东西,但效果不太好。