4

I have this code

$('#postinput').on('keyup',function(){ 
    var txt=$(this).val();

    $.ajax({
       type: "POST",
       url: "action.php",
       data: 'txt='+txt,
       cache: false,
       context:this,
       success: function(html)
       {
           alert(html);
       }

   });

});


$('#postinput2').on('keyup',function(){ 
    var txt2=$(this).val();

    $.ajax({
       type: "POST",
       url: "action.php",
       data: 'txt2='+txt2,
       cache: false,
       context:this,
       success: function(html)
       {
           alert(html);
       }

   });

});

Suppose user clicked on #postinput and it takes 30 seconds to process.If in the meantime user clicks on #postinput2 . I want to give him an alert "Still Processing Your Previous request" . Is there a way i can check if some ajax is still in processing?

Suppose I have lot of ajax running on the page. Is there a method to know if even a single one is in processing?

4
  • So, quick (and possibly dirty) idea of mine is: just use a global boolean "isProcessing" and set it to true/false while processing / in success method of ajax. Edit: Ok, now there's the same thing as an answer ;) Commented Oct 29, 2013 at 13:10
  • @Dominik tnx.. if i have a number of ajax on same page suppose 5. This true & false method won't work then. Is there any method to check if even 1 ajax is in progress..? Commented Oct 29, 2013 at 13:16
  • 1
    Just check $.active property. It's not mentioned in the official documentation, and it might change later, but currently it's covered by unit tests, hasn't changed since 2010 and even has some bug tracking attached. ) Commented Oct 29, 2013 at 13:19
  • @raina77ow tnx $.active did the trick :) Commented Oct 29, 2013 at 13:27

1 Answer 1

6

You can set a variable to true or false depending on when an AJAX call starts, example:

var ajaxInProgress = false;

$('#postinput2').on('keyup',function(){ 
    var txt2=$(this).val();

    ajaxInProgress = true;
    $.ajax({
      ..
      ..
      success: function(html) {
          ajaxInProgress = false;

Now check it if you need to before a call:

if (ajaxInProgress)
    alert("AJAX in progress!");

Or, use global AJAX events to set the variable

$( document ).ajaxStart(function() {
   ajaxInProgress = true;
});

$( document ).ajaxStop(function() {
   ajaxInProgress = false;
});
Sign up to request clarification or add additional context in comments.

5 Comments

@RoryMcCrossan - Did not even think of that :\ -- I was hoping with OP's logic that 2 would never run concurrent, but there's a chance..hmm..ideas?
tnx.. I wanted to ask if i have a number of ajax on same page suppose 5. This true & false method won't work then. Is there any method to check if even 1 ajax is in progress..?
Ehh not that I know of
You could increase a counter in ajaxStart and decrease it in ajaxStop, when its >0 you know there are pending ajax calls.
That's all done by jQuery internally, no need to duplicate this functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.