【问题标题】:What does $.active means in following jQuery code? [duplicate]$.active 在下面的 jQuery 代码中意味着什么? [复制]
【发布时间】:2019-06-10 15:25:49
【问题描述】:

我在我的项目中使用以下 jQuery 库。以下是添加它们的代码:

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

现在前面开发者写的函数如下:

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();
      }
    });
//}
}

现在有人可以解释一下以下文件的作用和重要性吗?

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

提前致谢。

【问题讨论】:

标签: jquery


【解决方案1】:

jQuery $.active 是 jQuery 内部使用但官方文档中没有提及的函数。但是在我们编码的时候使用in是没有问题的。

它是启用ajaxStart()ajaxStop() 的全局事件处理程序的标志。您可以找到有关该here 的更多详细信息并阅读this 问题。

这是在github上找到的一些代码sn-ps。

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");
}

【讨论】:

    【解决方案2】:

    它测试是否有活动连接,如果有(> 0),它将执行函数request_inprogress(),否则将执行语句的else部分。

    它可能是为了防止代码在已经处理时被处理。

    【讨论】:

      【解决方案3】:

      jQuery.active

      这里描述了测试到服务器的活动连接数,当连接数为零时将评估为真。

      【讨论】:

      • 因此,如果我删除此代码,那么其余功能将正常工作。还是整个功能代码中必要且重要的部分?
      • 它帮助我们提供与其他正在进行的事件同步的视觉效果。仅当“'else' 部分过程与方法 request_inprogress(); 无关”时,您才能将其删除。
      • jQuery.active 在连接为零时会是假的,我希望。没有?
      猜你喜欢
      • 2012-05-02
      • 1970-01-01
      • 2021-12-16
      • 2011-10-14
      • 2021-11-04
      • 1970-01-01
      • 2020-09-24
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多