5

I'm using following jQuery libraries in my project. Following is the code for adding them:

<script src="jquery-1.9.1.min.js"></script>
    <script src="jquery-ui-1.10.0.custom.min.js"></script>

Now the function which is written by previous developer as follows:

function add_rebate_by_product() {
  if($.active > 0) { //or $.active  //What this $.active does   
    request_inprogress();
  } else {
    var manufacturer_id = $("#company_id").val();
    var rebate_no       = $('.well').length;
    var site_url        = $('#site_url').val();

    if ($('.well').length>=0) { 
      rebate_no = rebate_no+1;
    }
      $('.add_new_rebate').attr('disabled','disabled');
    }

    $.ajax({
      type: "POST",
      url: "add_rebate_by_product.php",
      data: {'request_type':'ajax', 'op':'create_rebate', 'rebate_no':rebate_no, 'manufacturer_id':manufacturer_id},  
      beforeSend: function() { 
        $('.load_img').html("<img src='"+site_url+"img/ajax-loader.gif' class='load' alt='Loading...'>");
      },
      success: function(data) {
        if(jQuery.trim(data)=="session_time_out") {
          window.location.href = site_url+'admin/login.php?timeout=1';              
        } else {
          $('.rbt').append(data);
          $('.add_new_rebate').removeAttr('disabled');

          //code for state select control to make functionality workable in newly added rebate block
          $('.states').multiselect({
            includeSelectAllOption: true,
            maxHeight: 150
          });
          //code for datepicker control to make functionality workable in newly added rebate block
          $(".date_control").datepicker({
            dateFormat: "yy-mm-dd"
          });                   
        }
        $('.load').remove();
      }
    });
//}
}

Now can someone please explain what the following files does and what's it's importance?

if($.active > 0) { //or $.active        
        request_inprogress();
      }

Thanks in advance.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
PHPLover
  • 1
  • 51
  • 158
  • 311

3 Answers3

8

jQuery $.active is a function which jQuery uses internally but not mentioned in the official documentation. But there is no problem in using in when we code.

It is a flag to enable global event handlers for ajaxStart() and ajaxStop(). You can find more details on that here and reading this question.

Here are some code snippets found on github.

1.

// Counter for holding the number of active queries
active: 0,

2.

// Watch for a new set of requests
if ( fireGlobals && jQuery.active++ === 0 ) {
    jQuery.event.trigger("ajaxStart");
}

3.

// Handle the global AJAX counter
if ( !( --jQuery.active ) ) {
    jQuery.event.trigger("ajaxStop");
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Maleen Abewardana
  • 13,600
  • 4
  • 36
  • 39
4

It tests to see if there is an active connection, if there are any ( > 0), it will perform function request_inprogress(), otherwise it will peform the else part of the statement.

It is probably there to prevent the code from processing, while already processing.

RST
  • 3,899
  • 2
  • 20
  • 33
3

jQuery.active

It is described here to test the number of active connections to a server and will evaluate true when the number of connections is zero.

Bhumin Vadalia
  • 271
  • 1
  • 5
  • So, if I remove this code then will the rest of functionality work. Or is it necessary and important part of the whole function code? – PHPLover Aug 12 '14 at 07:59
  • It helps us to give a visual of synchronization with the other on going Events. You can remove it only IF "the 'else' part procedure has nothing to with the method request_inprogress();" – Bhumin Vadalia Aug 12 '14 at 08:30
  • 1
    `jQuery.active` will be falsey when there are zero connections, I'd expect. No? – flup Aug 30 '14 at 20:02