【问题标题】:How to call .ajaxStart() on specific ajax calls如何在特定的 ajax 调用中调用 .ajaxStart()
【发布时间】:2010-11-14 13:23:01
【问题描述】:

我在网站文档上有一些 ajax 调用,根据 ajax 状态显示或隐藏进度条

  $(document).ajaxStart(function(){ 
        $('#ajaxProgress').show(); 
    });
  $(document).ajaxStop(function(){ 
        $('#ajaxProgress').hide(); 
    });

我想基本上在站点的其他部分重写这些方法,这些部分进行了很多快速的小型 ajax 调用,并且不需要进度条弹出和弹出。我正在尝试将它们附加到或插入到其他 $.getJSON 和 $.ajax 调用中。我曾尝试将它们链接起来,但显然这并不好。

$.getJSON().ajaxStart(function(){ 'kill preloader'});

【问题讨论】:

标签: jquery ajax progress-bar preloader


【解决方案1】:

2018 注意:此答案已过时;随时建议对此答案进行编辑。

您可以使用自定义命名空间绑定 ajaxStart 和 ajaxStop:

$(document).bind("ajaxStart.mine", function() {
    $('#ajaxProgress').show();
});

$(document).bind("ajaxStop.mine", function() {
    $('#ajaxProgress').hide();
});

然后,您将在站点的其他部分您的 .json 调用之前暂时解除它们的绑定:

$(document).unbind(".mine");

在寻找答案时从here 得到这个想法。

编辑:唉,我还没来得及测试它。

【讨论】:

  • 如果我使用 $(document).unbind(".mine");在 JSON 调用之前,之后如何正确重新绑定命名空间?
  • @nights 这个答案来自 2009 年。您可以随时提出替代方案,以便我进行相应的编辑。
【解决方案2】:

选项对象 .ajax() 中有一个名为 global 的属性。

如果设置为false,则不会触发调用的ajaxStart事件。

    $.ajax({
        url: "google.com",
        type: "GET",
        dataType: "json",
        cache: false,
        global: false, 
        success: function (data) {

【讨论】:

    【解决方案3】:

    使用本地范围的 Ajax 事件

                    success: function (jQxhr, errorCode, errorThrown) {
                        alert("Error : " + errorThrown);
                    },
                    beforeSend: function () {
                        $("#loading-image").show();
                    },
                    complete: function () {
                        $("#loading-image").hide();
                    }
    

    【讨论】:

      【解决方案4】:

      像这样在 ajax 调用中使用 beforeSend 或完成回调函数..... 现场示例在这里 https://stackoverflow.com/a/34940340/5361795

      来源ShoutingCode

      【讨论】:

        【解决方案5】:

        我有一个解决方案。 我设置了一个名为 showloader 的全局 js 变量(默认设置为 false)。在您想要显示加载程序的任何函数中,只需在进行 ajax 调用之前将其设置为 true。

        function doAjaxThing()
        {
            showloader=true;
            $.ajax({yaddayadda});
        }
        

        然后我的头部有以下内容;

        $(document).ajaxStart(function()
        {
            if (showloader)
            {
                $('.loadingholder').fadeIn(200);
            }
        });    
        
        $(document).ajaxComplete(function() 
        {
            $('.loadingholder').fadeOut(200);
            showloader=false;
        });
        

        【讨论】:

          【解决方案6】:
          <div class="Local">Trigger</div>
          
          <div class="result"></div>
          <div class="log"></div>
          
          $(document).ajaxStart(function() {
          $( "log" )text( "Trigger Fire successfully." );
          });
          
          $( ".local" ).click(function() {
          $( ".result" ).load("c:/refresh.html.");
          });
          

          通过这个例子你会有所了解。当用户点击类Local的元素并发送Ajax请求时,会显示日志消息。

          【讨论】:

            【解决方案7】:

            如果您将其放入处理 ajax 操作的函数中,它只会在适当时绑定自身:

            $('#yourDiv')
                .ajaxStart(function(){
                    $("ResultsDiv").hide();
                    $(this).show();
                })
                .ajaxStop(function(){
                    $(this).hide();
                    $(this).unbind("ajaxStart");
                });
            

            【讨论】:

            • 谢谢!我发现你的答案是最容易实现的。
            • 这行得通吗?您可以在自己的选择器上使用 ajaxStart 吗?来自文档:As of jQuery 1.8, the .ajaxStart() method should only be attached to document.
            【解决方案8】:

            如果您想在决定做什么之前检查请求,请使用 ajaxSend 和 ajaxComplete。在这里查看我的回复:https://stackoverflow.com/a/15763341/117268

            【讨论】:

              【解决方案9】:

              此外,如果您想禁用.ajaxStart().ajaxStop()的调用,您可以在.ajax()请求中将global选项设置为false;)

              在此处查看更多信息:How to call .ajaxStart() on specific ajax calls

              【讨论】:

              • 这应该是正确的答案。它使您可以停止显示特定请求的进度条,而无需更改所有代码。
              【解决方案10】:

              很遗憾,ajaxStart 事件没有任何附加信息可用于决定是否显示动画。

              无论如何,这是一个想法。在您的 ajaxStart 方法中,为什么不在 200 毫秒后开始动画?如果 ajax 请求在 200 毫秒内完成,则不显示任何动画,否则显示动画。代码可能类似于:

              var animationController = function animationController()
              {
                  var timeout = null;
                  var delayBy = 200; //Number of milliseconds to wait before ajax animation starts.
              
                  var pub = {};
              
                  var actualAnimationStart = function actualAnimationStart()
                  {
                      $('#ajaxProgress').show();
                  };
              
                  var actualAnimationStop = function actualAnimationStop()
                  {
                      $('#ajaxProgress').hide();
                  };
              
                  pub.startAnimation = function animationController$startAnimation() 
                  { 
                      timeout = setTimeout(actualAnimationStart, delayBy);
                  };
              
                  pub.stopAnimation = function animationController$stopAnimation()
                  {
                      //If ajax call finishes before the timeout occurs, we wouldn't have 
                      //shown any animation.
                      clearTimeout(timeout);
                      actualAnimationStop();
                  }
              
                  return pub;
              }();
              
              
              $(document).ready(
                  function()
                  {
                      $(document).ajaxStart(animationController.startAnimation);
                      $(document).ajaxStop(animationController.stopAnimation);
                  }
               );
              

              【讨论】:

                猜你喜欢
                • 2014-10-21
                • 1970-01-01
                • 2011-10-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多