javascript 包含javascript的自动刷新div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19703289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
auto refresh div containing javascript
提问by Mashael
I want to refresh a DIV that has a JavaScript inside it every 5 seconds. I tried this code, but it only refreshes one time. how can I make it execute the JavaScript code every five seconds ?
我想每 5 秒刷新一个里面有 JavaScript 的 DIV。我试过这段代码,但它只刷新一次。如何让它每五秒执行一次 JavaScript 代码?
<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>
回答by John
I found this answer to another question here: Auto Refresh DIV contents every 5 seconds code not working
我在这里找到了另一个问题的答案:每 5 秒自动刷新 DIV 内容代码不起作用
I think your refresh function is incomplete, for instance there's nothing that makes it loop. Try something like this:
我认为您的刷新功能不完整,例如没有任何东西可以让它循环。尝试这样的事情:
$(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();
});
回答by Sridhar R
Try this
试试这个
$(document).ready(function(){
setInterval(function(){
$('#content').load('count.html');
}, 5000);
});