location.reload(真);不在ie11工作

问题描述 投票:0回答:3

我有一个脚本,当值> = 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);
}

});

javascript jquery ajax internet-explorer
3个回答
0
投票

您似乎没有在任何地方定义self,因此您可能会遇到错误。除此之外,你试图将href的值赋值为location的整个值 - 这意味着它是一个对象。相反,尝试:

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

0
投票

尝试将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);
  }
});

0
投票

把if放在success函数中,ajax是异步的if 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);
}
});
© www.soinside.com 2019 - 2024. All rights reserved.