2013-10-31 77 views
2

我想每5秒鐘刷新一次內含JavaScript的DIV。我試過這個代碼,但它只刷新一次。我怎樣才能讓它每五秒鐘執行一次JavaScript代碼?包含javascript的自動刷新div

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/ 
libs/jquery/1.3.0/jquery.min.js"></script> 
<script type="text/javascript"> 
var auto_refresh = setInterval(
function() 
{ 
$('#content').load('count.html'); 
}, 5000); // refresh every 5000 milliseconds 

</script> 
</head> 

<body> 

<div id="content"> 
    <script type="text/javascript"> 

    today = new Date(); 
    BigDay = new Date("December 25, 2020"); 
    msPerDay = 24 * 60 * 60 * 1000 ; 
    timeLeft = (BigDay.getTime() - today.getTime()); 
    e_daysLeft = timeLeft/msPerDay; 
    daysLeft = Math.floor(e_daysLeft); 
    e_hrsLeft = (e_daysLeft - daysLeft)*24; 
    hrsLeft = Math.floor(e_hrsLeft); 
    minsLeft = Math.floor((e_hrsLeft - hrsLeft)*60); 
    document.write("There are only<BR> <H4>" + daysLeft + " days " + hrsLeft +" hours and " + minsLeft + " minutes left </H4> Until December 25th 2020<P>"); 
    </script> 
</div> 

</body> 
</html> 
+0

當我代替你的'$( 「#內容」)負荷..。 http://codepen.io/anon/pen/hldLe – EricG

+0

請參閱本http://stackoverflow.com/questions/18503478/auto-refresh-div-contents-every-5-seconds-code-not-working –

回答

2

我發現這個答案在這裏另外一個問題:Auto Refresh DIV contents every 5 seconds code not working


我覺得你的刷新功能不完善,比如沒有什麼,使得它循環。嘗試是這樣的:

$(document).ready(function() { 
var seconds = 5000; // time in milliseconds 
var reload = function() { 
    $.ajax({ 
     url: 'editStatus.jsp', 
     cache: false, 
     success: function(data) { 
      $('#refreshDIV').html(data); 
      setTimeout(function() { 
      reload(); 
      }, seconds); 
     } 
    }); 
}; 
reload(); 
}); 
+0

我試着你的代碼,但它不會刷新div內容。 – Mashael

+0

這是垃圾郵件的答案,你可以看到這個http://stackoverflow.com/questions/18503478/auto-refresh-div-contents-every-5-seconds-code-not-working –

+0

@Sandeep當您複製內容的副本從其他地方,你**必須*來源。此外,您不應該簡單地複製粘貼整個事情,而是將自己的措辭包含在答案中,並將參考信息僅用於支持。 –

0

()``通過警報()`,它工作正常試試這個

$(document).ready(function(){ 
setInterval(function(){ 
    $('#content').load('count.html'); 
    }, 5000); 
});