admin管理员组

文章数量:1336137

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?

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?

Share Improve this question edited Oct 29, 2013 at 13:19 Ace asked Oct 29, 2013 at 13:06 AceAce 84510 silver badges23 bronze badges 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 ;) – Dominik 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..? – Ace 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. ) – raina77ow Commented Oct 29, 2013 at 13:19
  • @raina77ow tnx $.active did the trick :) – Ace Commented Oct 29, 2013 at 13:27
Add a ment  | 

1 Answer 1

Reset to default 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;
});

本文标签: javascriptCheck if some ajax on the page is in processingStack Overflow