Is there any way to detect if there is any asynchronous call being routed?

3

I need to loop asynchronous calls, but they can not run simultaneously, I have to wait for the end to run another. I just do not have control over this call ( It's a js function Sharepoint CSOM ).

So I at the beginning of my idea is just to detect if a call is already running. If it is running I will test again until there is no more call running, and then yes I continue the loop.

More or less the idea:

function chamarAjax(){
    blablabla();

    if(blablabla()){
        continuarLoop = false;
    }
}

if(!ajaxIsRunning){
  if(continuarLoop){
      chamarAjax();
  }
}
    
asked by anonymous 26.01.2015 / 13:59

4 answers

1

Count how many times you execute an AJAX request, and then count the number of times you have seen a call to the completed callback. Once the completed callback equals the number of times you send ajax calls, you'll know what's running.

var total = arr.length;
var count = 0;
for(var i = 0; i < arr.length; i++){
     $.ajax({
        // outras opções 
        complete: function(){
            count++;
            if(count == total){ 
                // tudo consumado!
            }
        }
     });
}

Note that I use the "full" callback, it is not "successful," since if one of the requests fails, it is 'complete'. In addition, we declared the expected value in 'total' first. This is to avoid the unlikely (though technically possible) scenario of having all pending ajax requests ending before you post all of them, and therefore have a count.

    
26.01.2015 / 14:10
1

You can simply chain your calls, just call the next one when you have finished the previous one, I do not know if this meets your needs, but it's a way:

$(function(){
    $.ajax({ // request 1
        type: "POST",
        url: "some.php",
        data: { name: "John", location: "Boston" }
    }).always(function() {
        console.log("Complete 1");
        $.ajax({ // request 2
            type: "POST",
            url: "some2.php",
            data: { name: "John", location: "Boston" }
        }).always(function() {
            console.log("Complete 2");
            $.ajax({ // request 3
                type: "POST",
                url: "some3.php",
                data: { name: "John", location: "Boston" }
            }).always(function() {
                console.log("Complete 3");
            });
        });
    });
});
    
26.01.2015 / 14:46
1

If you are using jQuery, you can get the number of active Ajax requests using the $.active function, this is a function that jQuery uses internally but not mentioned in the official documentation. It's okay to use it.

See this example found in Github .

function checkPendingRequest() 
{
    if ($.active > 0) {
      // existe requisições pendentes
    }
    else {

      // não há requisições pendentes
    }
};

References: Source¹ , Source²

    
26.01.2015 / 14:53
0

You can set the async property of AJAX to false , is a property that already exists in AJAX of JQuery

By default, the jQuery.Ajax method comes with the parameter async set to true . This means that assíncronas requests are enabled. Sometimes it is necessary that we bind the requests Ajax , that is, to make a request Ajax only when the previous one is finished, and then, to carry out the next one.

More or less this way:

Thatis,síncronasrequestsarebeingdiscouraged,butyoucanstillusetheminthisway,andforalongtimeitwillstillbepossible

InthiswayyourAjaxcouldlooksomethinglikethis:

jQuery.ajax({/*Requisição1*/url:'index.html',async:false,success:function(data){jQuery('.destino').html(data);jQuery.ajax({/*Requisição2*/url:'index2.html',async:false,success:function(){alert(jQuery("#campo").val());
     }

 }
});
    
26.01.2015 / 14:38