jQuery:我应该使用多个ajaxStart/ajaxStop处理吗?

18

也许没有什么区别,但是其中一种方式比另一种更好(或者可能存在一个神秘的“第三种”方式比两种都更好!)...


first:

var startTime;

$(document).ready(function() {

    $("#lbl_ajaxInProgress").ajaxStart(function() {
        // store the current date/time...
        startTime = new Date();
        // update labels
        $(this).text('Yes');
        $("#lbl_ajaxCallTime").text("-");
    });

    $("#lbl_ajaxInProgress").ajaxStop(function() {
        // update labels
        $(this).text('No');
        $("#lbl_ajaxCallTime").text(myFunctionThatCalculatesTime(startTime));
    });

});
var startTime;

$(document).ready(function() {

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

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

    $("#lbl_ajaxCallTime").ajaxStart(function() {
        // store the current date/time...
        startTime = new Date();
        // update labels
        $(this).text("-");
    });

    $("#lbl_ajaxCallTime").ajaxStop(function() {
        // update labels
        $(this).text(myFunctionThatCalculatesTime(startTime));
    });

});

1
从jQuery 1.8开始,.ajaxStart()方法只能附加到文档。 - ThdK
这个回答解决了你的问题吗?禁用特定请求的ajaxStart()和ajaxStop() - ahmet
2个回答

43

有趣的事实是,ajaxStart等都只是jQuery事件。例如:

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

等同于:

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

这意味着您也可以将命名空间附加到ajaxStart/ajaxStop等事件上。 这也意味着您可以执行以下操作:

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

你也可以这样做:

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

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

然后:

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

很酷,是吧?


当然可以!我这里假设一下,你的意思是无论哪种方式都可以吗? - davidsleeps
ajaxStart帮助程序相当于click帮助程序,它仅委托给bind,因此无论哪种方式都无所谓。 - Yehuda Katz
2
从jQuery 1.8开始,.ajaxStart()方法只应该附加到文档。 - ThdK
这个解决方案现在不起作用了,因为jQuery带来了新的规则。 - yogihosting

4

这是使用Ajax的正确方式...

<!DOCTYPE html>
<html lang="en">
<head>
<title>Shouting Code</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
 href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<script
 src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script
 src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">
  </script>
</head>
<body>
 <button type="submit" class="btn btn-default"
  onclick="ajaxRequest(this);">
  <i class="fa fa-refresh"></i> Ajax Call
 </button>
 <script>
  function ajaxRequest(id) 
    {
      // ajax request
        $.ajax({
            type: 'post',
            url: '/echo/html/',
            data: {
                html: '<p>This is echoed the response in HTML format</p>',
                delay: 600
            },
            dataType: 'html',
            beforeSend: function() { 
                //  alert("start");
    $(id).find('i').addClass('fa-spin');
   },
            success: function(data) {
                alert('Fired when the request is successfull');
            },
            complete:function(){  
                 //   alert("stop");
    $(id).find('i').removeClass('fa-spin');
   }
        });
}</script>
</body>
</html>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接