Clearinterval无法在ajaxrequest中工作

时间:2012-06-27 13:38:11

标签: php javascript ajax

我做了一个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);    
    }

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

2 个答案:

答案 0 :(得分:2)

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

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

您现在的方式,prog在您拨打clearInterval()

时未定义{{1}}

答案 1 :(得分: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则意味着它尚未设置且不需要清除。