jQuery should I use multiple ajaxStart/ajaxStop handling

后端 未结 2 503
感情败类
感情败类 2020-12-04 16:30

Maybe there is no difference, but is either way better than the other (or perhaps a mysterious \'third\' way better than both!)...


first:

var         


        
2条回答
  •  伪装坚强ぢ
    2020-12-04 16:50

    An interesting fact is that ajaxStart, etc. are actually just jQuery events. For instance:

    $("#lbl_ajaxInProgress").ajaxStart(function() {
      // update labels
      $(this).text('Yes');
    });
    

    is equivalent to:

    $("#lbl_ajaxInProgress").bind("ajaxStart", function() {
      // update labels
      $(this).text('Yes');
    });
    

    This means that you can also attach namespaces to ajaxStart/ajaxStop, etc. Which also means that you can do:

    $("#lbl_ajaxInProgress").unbind("ajaxStart ajaxStop");
    

    You could also do:

    $("#lbl_ajaxInProgress").bind("ajaxStart.label", function() {
      // update labels
      $(this).text('Yes');
    });
    
    $("#lbl_ajaxInProgress").bind("ajaxStop.label", function() {
      // update labels
      $(this).text('No');
    });
    

    And then:

    $("#lbl_ajaxInProgress").unbind(".label");
    

    Cool, huh?

提交回复
热议问题