gpt4 book ai didi

javascript - 包含javascript的自动刷新div

转载 作者:行者123 更新时间:2023-11-29 22:08:15 25 4
gpt4 key购买 nike

我想每 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>

最佳答案

我在这里找到了另一个问题的答案:Auto Refresh DIV contents every 5 seconds code not working


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();
});

关于javascript - 包含javascript的自动刷新div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19703289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com