javascript - Safari 浏览器在 window.location 之后停止 js 执行

标签 javascript jquery safari

我有 js 代码,它在单击按钮时显示(假)进度条。 与设置超时相比,它会改变进度条的值

 $(".quickbook-btn").click(function(){
   $(".progress").removeClass('hide')
   setTimeout(barAnim, 50);
   window.location = path
 });

 var value = 0;
 function barAnim(){
   value += 5;
   $( ".progress-bar" ).css( "width", value + "%" ).attr( "aria-valuenow", value );
   $(".progress-bar").text((value)+'%');
   if ( value == 10 || value == 20 || value == 30|| value == 50 || value == 75 || value == 90 ) {
     return setTimeout(barAnim, 1500);
   }
 }

它在 Chrome、Firefox 等中完美运行。

但是当涉及到 Safari 时,它会在到达 window.location 行时停止 js 执行。

我什至测试过它,让reach进度达到50%,然后调用window.location,它会立即停止执行js,尽管window.location中的url需要10多秒才能完成。

这里有什么问题吗?为什么 Safari 总是很糟糕

最佳答案

在找不到合适的解决方案后,我将请求转移到 ajax 并在响应时使用 window.location。 (但有一点是肯定的,Safari 很糟糕)

$(".quickbook-btn").click(function(){
      $(".progress").removeClass('hide')
      setTimeout(barAnim, 50);

      $.ajax({
        method: "GET",
        url: "#{path}",
      }).done(function( data ) {
        $( ".progress-bar" ).css( "width", 100 + "%" ).attr( "aria-valuenow", 100 );
        $(".progress-bar").text((100)+'%');
        window.location = "#{club_quick_books_path}"
      });
    });

    var value = 0;
    function barAnim(){
        value += 5;
        $( ".progress-bar" ).css( "width", value + "%" ).attr( "aria-valuenow", value );
        $(".progress-bar").text((value)+'%');
        if ( value == 10 || value == 20  || value == 30|| value == 50 || value == 75 || value == 90 ) {
            return setTimeout(barAnim, 1500);
        }
        return value >= 95 || setTimeout(barAnim, 50);
    }

关于javascript - Safari 浏览器在 window.location 之后停止 js 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47886078/

相关文章:

javascript - 在 Angular Controller 中监听文档事件

javascript - 在 JavaScript 中使输入字​​段不区分大小写

javascript - 如何设置复杂的 jquery 函数?

javascript - 使用 jquery 改变背景位置时的抖动

css - Bootstrap 4 在 Safari 上选择不一样

html - 为特定浏览器加载不同的 CSS 样式 [不是 IE]

Javascript/Jquery : Most efficient way to find index of first and last array with key-value pair in nested array

javascript - 使用 Javascript math.max.apply(null 或 math..)

javascript - Jquery - 遍历所有选中的单选按钮

javascript - Safari ios 音频 - 拒绝播​​放错误