Jquery setInterval在Firefox上完美运行,但在IE8上不起作用

时间:2012-02-19 02:24:04

标签: jquery firefox internet-explorer-8 setinterval

我写了一个简单的代码来从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();

3 个答案:

答案 0 :(得分:1)

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

$.ajaxSetup({
    cache: false
});

答案 1 :(得分:0)

避免中间方法链中的换行符和注释;充其量只是丑陋,最糟糕的是并非所有的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);

答案 2 :(得分:0)

IE兑换所有获取请求,试试这个:

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