How to check if ajax function from previous call is busy

How to check if ajax function from previous call is busy? How can I prevent the ajax function call if it is readyState != 4 from the previous call?

+4
source share
2 answers

You can use the logical and appropriate functioning of onreadystatechange;

 var inprogress = true; ajaxRequest = ... ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ inprogress = false; } } 
+5
source

The question is, do you want to skip the Ajax call if the previous call is still busy or not? If you want to skip this solution, it is easy, and one of them is perfect for you. However, if you want to start making a call after completing the previous one, this is a completely different story. If necessary, I can provide you with a code.

Worked on the solution:

 <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script> <script language="javascript" type="text/javascript"> var requestInProcess = false; var timer = null; function requestSomeStuffA() { var responseHolder = $("responseHolder"); new Ajax.Request( "http://webservicelocation/", { method: 'GET', contentType: 'application/json; charset=utf-8', onCreate: function() { requestInProcess = true; }, onSuccess: function(transport) { //do something with the transport }, onFailure: function(error) { //throw a nice exception }, onComplete: function() { requestInProcess = false; } } ); } function requestSomeStuffB() { clearTimeout(timer); if(requestInProcess) { //wait again for a while timer = window.setTimeout("requestSomeStuffB()", 10); //timeout in miliseconds } else{ //execute the next Ajax request } } </script> 

When you call the first request of SomeStuffA and then requestSomeStuffB, requestSomeStuffB will wait for the completion of the requestSomeStuffA. Hope this is helpful for you.

0
source

Source: https://habr.com/ru/post/1285785/


All Articles