简体   繁体   English

如何停止ajax请求?

[英]how to stop ajax request?

I have multiple ajax requests and when one of them can't get data I want , I re-send it until it can get data . 我有多个ajax请求,当其中之一无法获取我想要的数据时,我会重新发送它,直到它可以获取数据为止。 the problem that I can't stop it after it gets data . 问题是我获取数据后无法停止它。 Is there's a break or something equivalent to it in ajax ? 在ajax中有一个休息或等同的东西吗? I tried clearinterval but it didn't work here's my functions : 我尝试了clearinterval,但是它不起作用,这是我的功能:

  function ajaxGetServerDatabase(Div,val,interval){
       console.log(val);
       dbs[val]=new Array();
        $('#bck_action').val('get_DB');
        $('#server_ip').val(val);
        post_data = $('#'+Div+' *').serialize();
        $.ajax({
            type: "POST",
            url: document.URL,
            data: post_data,

            dataType: "text",
            success: function(response) {

                if (response!='no_connection'){
                    dbs[val]=JSON.parse(response)
                    clearInterval(this.interval);  // ???? 
                }

            }
        });
        return false;
    }

  function ajaxGetDatabase(Div,ips,interval){
    $.each(ips,function(i,val){
       dbs[val]=new Array();
        $('#bck_action').val('get_DB');
        $('#server_ip').val(val);
        post_data = $('#'+Div+' *').serialize();
        //    console.log(post_data);
        $.ajax({
            type: "POST",
            url: document.URL,
            data: post_data,

            dataType: "text",
            success: function(response) {

                if (response!='no_connection'){
                    dbs[val]=JSON.parse(response)
                }
                else
               {
                   setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
               }
            }
        });
    });

    return false;
}

I call it : 我称之为:

  ajaxGetDatabase('tab_backup',ips,3000);
var timer = null;
function ajaxGetServerDatabase(Div,val,interval){
   //...
   if (response!='no_connection'){
                dbs[val]=JSON.parse(response)
                clearInterval(timer);  // ???? 
            }
   //....
   else
           {
               timer = setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
           }

clearInterval has nothing to do with ajax . clearIntervalajax无关。 It's only a timer function which scope is to clear the timer set earlier with setInterval . 这只是一个计时器函数,其作用范围是清除之前用setInterval设置的计时器。 If you really want to use a timer function you need either to attach a variable to the setInterval function, which you can clear with clearInterval setting as a parameter the id defined earlier in the setInterval . 如果您确实想使用计时器函数,则需要将一个变量附加到setInterval函数,您可以使用clearInterval设置作为参数清除setInterval先前定义的id。

var id = " ";
success: function(response) {

   if (response!='no_connection'){
       dbs[val]=JSON.parse(response)
       clearInterval(id);
   }
   else                   
       id= setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
}

Or you can abort the code with ajax abort . 或者,您可以使用ajax abort中止代码。

Maybe something close to this? 也许与此接近?

...
var stopAjax = 0;  //switch is on
if(stopAjax == 0){

  $.ajax({
    ...
    success: function(response) {
    if (response!='no_connection'){
       dbs[val]=JSON.parse(response);
       stopAjax = 1; //switch is off
    }
    else{
        setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"','"+interval+"')", interval);
      }
    }
  });
}

here's the answer : in ajaxGetDatabase : 这是答案:在ajaxGetDatabase中:

 success: function(response) {

                if (response!='no_connection'){
                    dbs[val]=JSON.parse(response)

                }
                else
                {
                 id=setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"')", interval);
            }

in ajaxGetServerDatabase : 在ajaxGetServerDatabase中:

    success: function(response) {
            if (response!='no_connection'){
                dbs[val]=JSON.parse(response)
                clearInterval(id);
            }

        }

with out scope parameter 不含范围参数

 var id;

to make it general and work for more than one server had stopped (more than one ajax request is failed) I used an array to save ids like this : 为了使它变得通用并在多个服务器停止工作(多个ajax请求失败)的情况下,我使用了一个数组来保存ID,如下所示:

   var ids=new Array();

   ids[val]=setInterval("ajaxGetServerDatabase('"+Div+"','"+val+"')", interval);

   clearInterval(ids[val]);    

Are you looking for the timeout setting perhaps? 您是否正在寻找的timeout可能设置? eg 例如

$.ajax({
    timeout: 10000,
    ...
});

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM