2012-02-19 107 views
0

我編寫了一個簡單的代碼來從PHP文件獲取內容並每隔30秒刷新一次。 它在FireFox上很漂亮,但在IE8中只加載一次內容! 可以任何身體幫助我解決它?!Jquery setInterval在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(); 
+0

具有相同代碼的重複副本是壞的。您應該將通用代碼放在一個函數中,並將其調用兩次,而不是複製/粘貼相同的代碼。 – jfriend00 2012-02-19 03:13:27

回答

1

IE將被緩存阿賈克斯的結果。在您的$ .get()調用之前放置:

$.ajaxSetup({ 
    cache: false 
}); 
+0

非常感謝.... – 2012-04-04 23:03:11

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); 
+0

非常感謝.... – 2012-04-04 23:03:01

0

IE兌現所有get請求,試試這個:

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