2016-07-23 42 views
4

我正在使用jQuery刷新「容器」中的內容,該內容每9秒刷新一次內容並從feed.php獲取數據並顯示在「容器」中。只有當服務器發生變化時,jQuery刷新div

只有在feed.php中有更改時刷新「容器」的最佳實踐,否則會顯示相同的內容?

簡單的HTML:

<body> 
<ul id="container"></ul> 
</body> 

<script> 
(function($) 
{ 
    $(document).ready(function() 
    { 
     $.ajaxSetup(
     { 
      cache: false, 
      beforeSend: function() { 
       $('#content').hide(); 
      }, 
      complete: function() { 
       $('#content').show(); 


      }, 
      success: function() { 
       $('#content').show(); 
      } 
     }); 
     var $container = $("#content"); 
     $container.load("feed.php"); 
     var refreshId = setInterval(function() 
     { 
      $container.load('feed.php'); 
     }, 9000); 
    }); 
})(jQuery); 

</script> 

回答

0

從PHP這個回報差異的狀態值像

$response[errCode] = 0; 
$response[errCode] = 1; 

和Ajax調用把一個條件,如:

success: function(response) { 
if(response == 0) 
    { 
     // refresh the container 
    } 
} 
相關問題