location.reload(true);不在IE11中工作



我有一个脚本,当值为> = 100时,问题是该location.reload(true);我在IE11中没有工作,我也尝试了window.location = self.location.href;但是我遇到了同样的问题,在其他浏览器中,它效果很好。

$(function () {
if (value < 100) {
    var timer = setInterval(function () {
        $.ajax({
            type: "GET",
            url: $("#ancUrl").attr('href'),
            data: {},
            success: function (msg) {
                console.log("This is msg:" + msg);
                var msgInt = parseInt(msg);
                if (msgInt > value)
                    value = msgInt;
            },
            error: function (err) {
                console.log(err.responseText);
            },
            dataType: "json"
        });
        $("#progress-bar").width(value.toString() + "%");
        if (value >= 100) {
            clearInterval(timer);
            window.location = self.location.href;
        }
    }, 2000);
}

});

您似乎在任何地方都没有定义self,因此您可能会有错误。除此之外,您还试图将href的值分配为location的整体值 - 这是一个对象。而是尝试:

window.location.href = window.location.href;

尝试将if语句移动到成功回调中。

这样您可以将间隔清除到同一堆栈中,然后在良好的页面上重新加载页面。

$(function() {
  if (value < 100) {
    var timer = setInterval(function() {
      $.ajax({
        type: "GET",
        url: $("#ancUrl").attr('href'),
        data: {},
        success: function(msg) {
          console.log("This is msg:" + msg);
          var msgInt = parseInt(msg);
          if (msgInt > value)
            value = msgInt;
          $("#progress-bar").width(value.toString() + "%");
          if (value >= 100) {
            clearInterval(timer);
            window.location = self.location.href;
          }
        },
        error: function(err) {
          clearInterval(timer);
          console.log(err.responseText);
        },
        dataType: "json"
      });
    }, 2000);
  }
});

将if放置在成功函数中,ajax是异步的,if将立即执行,但是在AJAX完成后,值将更改,以便代码永远不会到达IF语句

$(function () {
if (value < 100) {
    var timer = setInterval(function () {
        $.ajax({
            type: "GET",
            url: $("#ancUrl").attr('href'),
            data: {},
            success: function (msg) {
                console.log("This is msg:" + msg);
                var msgInt = parseInt(msg);
                if (msgInt > value) {
                    value = msgInt;
                    $("#progress-bar").width(value.toString() + "%");
                    if (value >= 100) {
                       clearInterval(timer);
                       location.reload(true);
                     }
                }
            },
            error: function (err) {
                console.log(err.responseText);
            },
            dataType: "json"
        });

    }, 2000);
}
});

最新更新