0

我进行了 ajax 调用并调用了一个运行正常的 setInterval 函数。不幸的是,当我试图阻止它时,它不起作用。

    function test(str)
        {
        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)
            {
                if (str=="?stage=3"){
                    test('?stage=4');
                } 
                if (str=="?stage=4"){
                     document.getElementById("main").innerHTML+=xmlhttp.responseText;
 prog=window.clearInterval(prog);

                }else{
                    document.getElementById("main").innerHTML+=xmlhttp.responseText;        
                }

            }
          else
          {
            if (str=="?stage=3"){
                var prog = self.setInterval( "ajaxrequest('progress_track.php', 'main')", 1000 );
            } 
        }
          }
        xmlhttp.open("GET","test.php"+str,true);
        xmlhttp.send();
        }

非常感谢任何帮助。

编辑:

我重新编写了我的代码,因为我发现它以某种方式调用了 setinveral 函数的 3 倍。这段代码只调用了 2 次。我只是不明白为什么。

var prog = 0;
function test(str)
{
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 && str=="?stage=3")
    {
        test('?stage=4');   
    }else{
        if( str=="?stage=3"){
        prog = self.setInterval( "ajaxrequest('progress_track.php', 'main')", 1000 );   
        }
    }
    if (xmlhttp.readyState==4 && xmlhttp.status==200 && str=="?stage=4")
    {
        prog=window.clearInterval(prog);    
    }
    if (xmlhttp.readyState==4 && xmlhttp.status==200 && str!="?stage=4" && str!="?stage=3")
    {
        document.getElementById("main").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","test.php"+str,true);
xmlhttp.send();
}

但是cleariterval仍然无法正常工作。

编辑:

我发现了问题。使用这段代码,它永远不会处于这种状态:

if (xmlhttp.readyState==4 && xmlhttp.status==200 && str=="?stage=4")
    {
        prog=window.clearInterval(prog);    
    }

现在唯一的问题是为什么不呢?

4

2 回答 2

2

您需要在更高的范围内声明“prog”。您可以将其移到函数声明之外:

var prog;
function test(str) {
  ...
  if (str=="?stage=3"){
    prog = self.setInterval( 
      "ajaxrequest('progress_track.php', 'main')", 1000 
    );
  }
  ...
}

你现在得到它的方式,prog当你打电话时是不确定的clearInterval()

于 2012-06-27T13:41:36.400 回答
0

它不起作用的原因是因为您正在声明变量以将计时器 ID 保留在函数中,但您却试图在定义之前使用它。当您尝试清除它时,该变量未定义。您可以做的是在外部定义变量然后使用它。像这样的东西:

var prog = 0;

function test(str)
    {
    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)
        {
            if (str=="?stage=3"){
                test('?stage=4');
            } 
            if (str=="?stage=4"){
                 document.getElementById("main").innerHTML+=xmlhttp.responseText;
                 prog=window.clearInterval(prog);

            }else{
                document.getElementById("main").innerHTML+=xmlhttp.responseText;        
            }

        }
      else
      {
        if (str=="?stage=3"){
            prog = self.setInterval( "ajaxrequest('progress_track.php', 'main')", 1000 );
        } 
    }
      }
    xmlhttp.open("GET","test.php"+str,true);
    xmlhttp.send();
}

您可能还想在清除之前检查它是否为 0。如果为 0,则表示尚未设置,不需要清除。

于 2012-06-27T13:41:51.440 回答