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?

Ace
  • 841
  • 9
  • 23
  • 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 ;) – Dominik Oct 29 '13 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..? – Ace Oct 29 '13 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](https://github.com/jquery/jquery/blob/705216dc4664a85cdb44b460f8c2e0edcf27dd97/test/unit/ajax.js#L1957), hasn't changed [since 2010](http://stackoverflow.com/questions/3148225/jquery-active-function) and even has some bug tracking attached. ) – raina77ow Oct 29 '13 at 13:19
  • @raina77ow tnx $.active did the trick :) – Ace Oct 29 '13 at 13:27

1 Answers1

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;
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • @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? – tymeJV Oct 29 '13 at 13:14
  • 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..? – Ace Oct 29 '13 at 13:15
  • Ehh not that I know of – tymeJV Oct 29 '13 at 13:18
  • You could increase a counter in ajaxStart and decrease it in ajaxStop, when its >0 you know there are pending ajax calls. – darthmaim Oct 29 '13 at 13:20
  • That's all done by jQuery internally, no need to duplicate this functionality. – raina77ow Oct 29 '13 at 13:23