【问题标题】:Chrome and IE not showing ajax loaderr gifChrome 和 IE 不显示 ajax 加载程序 gif
【发布时间】:2012-04-16 23:51:18
【问题描述】:

好的,我有一个执行 14 次 ajax 调用的函数。在循环之前我显示加载器 div,当循环结束时我隐藏加载器循环。这仅适用于 FF,不适用于 IE 和 Chrome。这是为什么?我尝试了两种不同的方法,但仍然没有得到任何结果。

方法一:

function myFunction(x,y,z)
{       
    $(".loader").show();

    for( 1 to 14 loop ) 
    {   
        $.ajax({
            type: 'GET',
            url: 'some.php',
            data: 'my data string here',
            async: false,
            dataType: 'html',
            //timeout: 30000,
            success: function(data)
            {
             // do some stuff (show/hide divs, increase some counters, some maths, etc) 
            }
        }); 

    }

    $(".loader").hide();

}

方法二

Same as approach 1, but:
- remove show/hide loader div from function
- add to my document ready the following code:

    $(".loader")
      .hide()
      .ajaxStart(function() { $(this).show(); })
      .ajaxStop(function() { $(this).hide(); });

这两种方法都适用于 Firefox,但没有一种适用于 IE 和 Chrome。为什么?我怎样才能让它跨浏览器工作?谢谢你的建议。

编辑:

  • 控制台没有错误(firefox 或 chrome)

  • 如果我将 asynch 设置为 true,我在任何浏览器中都看不到加载程序,尽管我看到 ajax 调用正在控制台中执行

请求后,这里是实际代码:

function nearBydates(depdate, depPort, arrPort, triptype, direction, route, count)
{

    var datesArray = new Array();
    var date = new Date();
    var parts = depdate.split('-');

    $(".loader").show();
    var oddEven = 'row1';

    // find dates -1 week
    var earlierCounter = 0;
    for(var i=14; i>=1; i--) 
    {
        var d = Date.parse(depdate);
        var nextDay = d.add(-i).day();
        var stringDate = nextDay.toString("yyyy-MM-dd");

        $.ajax({
            type: 'GET',
            url: 'page.php',
            data: 'depDate='+stringDate+"&depPort="+depPort+"&arrPort="+arrPort+'&triptype='+triptype+'&direction='+direction+'&route='+route+'&count='+count+"&class="+oddEven,
            async: false,
            dataType: 'html',
            //timeout: 30000,
            success: function(data)
            {
                if (data!='')
                {
                    $("#"+depPort+"_"+arrPort+"_holder").show();    
                    $("#"+depPort+"_"+arrPort+"_routes").append(data);
                    earlierCounter++;

                    if (earlierCounter == 1)
                    {
                        $("#you-can-options-up").show();
                        $("#you-can-options-bottom").show();
                    }

                    if ( oddEven == 'row1' ) { oddEven = 'row2'; } else { oddEven = 'row1'; }
                }               
            }
        });


    }


    if (  $("#"+depPort+"_"+arrPort+"_routes").html().length > 0  )
    {

    }
    else
    {
        $("#"+depPort+"_"+arrPort+"_error").show();
    }

    $("#"+depPort+"_"+arrPort).animate({height: '0px'}); 
    $("#"+depPort+"_"+arrPort).hide();  
    $(".loader").hide();

}

【问题讨论】:

  • 控制台中是否有任何错误/异常?第一种方法不起作用,因为加载器在任何 ajax 请求返回之前被隐藏(它们是异步的)。
  • @hamczu 在控制台(firefox 或 chrome)中没有发生错误。我不明白你的评论。我在初始化任何 ajax 调用之前手动显示加载器 gif(并在最后手动隐藏它)。如果我将 asynch 设置为 true,即使在 Firefox 中我也看不到任何加载器。好像显示/隐藏永远不会执行。
  • 你能显示for( 1 to 14 loop )的实际代码吗?如果将 async 更改为 true,则在 ajax 请求返回之前隐藏加载程序(如 xdazz 所指出的那样)。
  • @hamczu 我在我的问题中添加了原始脚本 - 但我不明白这将有什么帮助。这并不复杂 - 只是动态生成的 div 中的一些显示/隐藏数据

标签: jquery ajax internet-explorer google-chrome loader


【解决方案1】:

只需将加载程序放在显示结果集的位置,即

function myFunction(x,y,z)
{       
    $(".loader").html('loader.gif');

    for( 1 to 14 loop ) 
    {   
        $.ajax({
            type: 'GET',
            url: 'some.php',
            data: 'my data string here',
            async: false,
            dataType: 'html',
            //timeout: 30000,
            success: function(data)
            {
             $(".loader").html(data); 
            }
        }); 

    }

}

使用这种技术,结果将与加载器重叠

【讨论】:

  • 这个解决方案是错误的 - 加载器在第一个 ajax 请求返回后被隐藏。
  • @jassi9911 无法工作。您将 loader gif 替换为来自第一个 ajax 调用的 html 响应数据。然后将第一个响应的 html 数据替换为第二个响应。这不是我想要的。反正我问的也不是。
【解决方案2】:
function myFunction(x, y, z) {
    $(".loader").show();
    var finished_count = 0;

    for (1 to 14 loop) {
        $.ajax({
            type: 'GET',
            url: 'some.php',
            data: 'my data string here',
            dataType: 'html',
            //timeout: 30000,
            success: function (data) {
                // do some stuff (show/hide divs, increase some counters, some maths, etc)
                //count finished ajax request.
                finished_count++;
                if (finished_count === 14) {
                    $(".loader").hide();
                }
            }
        });
    }
}

【讨论】:

  • 抱歉 xdazz,虽然是个好主意,但它对我不起作用。我仍然在 chrome 中看不到加载器,即
  • 我认为这是您问题的正确答案,并且您的代码的其他地方有问题 - 可能是 html/css 问题。另外用async=false试试这个 - 我已经看到你在关闭时不执行ajax请求。
【解决方案3】:

希望对你有帮助

我有同样的问题,我真的不知道它是怎么发生的,但它可以 使用如下代码中的小延迟来修复。

解决方案 1

$.ajax({
  method: "GET",
  url: "/",
  beforeSend:function(){
    $(".loader").show(1);
    // please note i have added a delay of 1 millisecond , which runs almost same as code with no delay.
  },
  success:function(data){
    $(".loader").hide();
    //here i write success code
  }

});

解决方案 2

   $.ajax({
      method: "GET",
      url: "/",
      beforeSend:function(){

        setTimeout(function(){
           $(".loader-image").show();
        }, 1);
        // please note i have added a delay of 1 millisecond with js timeout function which runs almost same as code with no delay.
      },
      success:function(data){
        $(".loader-image").hide();
        //here i write success code
      }

    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-18
    • 2012-08-10
    • 1970-01-01
    • 2017-01-17
    • 2011-05-04
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多