【问题标题】:Jquery setInterval works perfectly on Firefox , but did not work on IE8Jquery setInterval 在 Firefox 上完美运行,但在 IE8 上无法运行
【发布时间】:2012-02-19 02:24:04
【问题描述】:

我编写了一个简单的代码来从 PHP 文件中获取内容并每 30 秒刷新一次。 它在 FireFox 上运行良好,但在 IE8 中只加载一次内容! 任何人都可以帮我解决它吗?!

这是我的代码:

<script>

var content;
var temp = "something";

    $.get('refresh.php', function(data) {
    content = data;
    })
  .success(function() {
      if (temp != content) {
          $("#success").fadeOut(2000, function () 
          {
              $("#success").html(content).fadeIn(2000); 
          }
          ); // end .fadeOut
      temp = content;
  }

  }) //end .success

  .error(function() { $("#success").html("error"); });

var refreshId = setInterval(function()
{
    $.get('refresh.php', function(data) {
    content = data;
    })
    .success(function() {
    if (temp != content) {
        $("#success").fadeOut(2000, function () 
        {
            $("#success").html(content).fadeIn(2000); 
        }
        ); // end .fadeOut
    temp = content;
    }

  }) //end .success

  .error(function() { $("#success").html("error"); })

}, 27000);
  </script>

在 PHP 代码上我有这个代码:

echo rand();

【问题讨论】:

  • 拥有相同代码的重复副本是不好的。您应该将通用代码放在一个函数中并调用它两次,而不是复制/粘贴相同的代码。

标签: jquery firefox internet-explorer-8 setinterval


【解决方案1】:

IE 将缓存 ajax 结果。把它放在你的 $.get() 调用之前:

$.ajaxSetup({
    cache: false
});

【讨论】:

    【解决方案2】:

    避免中间方法链中的换行符和 cmets;充其量只是丑陋,最坏的情况是并非所有 js 引擎都会发挥作用。另外,您可以将content 的声明移动到外部闭包中并缓存$("#success") 的结果。

    var refreshId = setInterval(function() {
        var $success = $("#success");
        var content;
        $.get('refresh.php', function(data) {
            content = data;
        }).success(function() {
            if (temp != content) {
                $success.fadeOut(2000, function () {
                    $success.html(content).fadeIn(2000); 
                });
                temp = content;
            }
        }).error(function() { $success.html("error"); });
    }, 27000);
    

    【讨论】:

      【解决方案3】:

      IE 兑现所有 get 请求,试试这个:

      $.get('refresh.php?'+ (new Date().getTime()), function(data) {
          content = data;
          })
      ...
      

      【讨论】:

        猜你喜欢
        • 2017-12-10
        • 2014-09-07
        • 1970-01-01
        • 1970-01-01
        • 2021-12-07
        • 1970-01-01
        • 1970-01-01
        • 2011-06-30
        • 2011-11-24
        相关资源
        最近更新 更多