我在标题中使用以下javascript来刷新页面
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
</script>
Run Code Online (Sandbox Code Playgroud)
并在身体标签
<body onload="JavaScript:timedRefresh(5000);">
Run Code Online (Sandbox Code Playgroud)
我的问题是如何添加一个显示倒计时的文本来刷新页面
x秒刷新
Pau*_*aul 20
这适合您的需求吗?
(function countdown(remaining) {
if(remaining <= 0)
location.reload(true);
document.getElementById('countdown').innerHTML = remaining;
setTimeout(function(){ countdown(remaining - 1); }, 1000);
})(5); // 5 seconds
Run Code Online (Sandbox Code Playgroud)
function timedRefresh(timeoutPeriod) {
var timer = setInterval(function() {
if (timeoutPeriod > 0) {
timeoutPeriod -= 1;
document.body.innerHTML = timeoutPeriod + ".." + "<br />";
// or
document.getElementById("countdown").innerHTML = timeoutPeriod + ".." + "<br />";
} else {
clearInterval(timer);
window.location.href = window.location.href;
};
}, 1000);
};
timedRefresh(10);
Run Code Online (Sandbox Code Playgroud)
我真的不明白你为什么要用setTimeout这个目的.
我知道这个问题已经在前一段时间内得到了回答,但我一直在寻找类似的代码,但间隔时间更长(分钟).它没有出现在我做的搜索中,所以这就是我提出的并且认为我会分享:
使用Javascript
function checklength(i) {
'use strict';
if (i < 10) {
i = "0" + i;
}
return i;
}
var minutes, seconds, count, counter, timer;
count = 601; //seconds
counter = setInterval(timer, 1000);
function timer() {
'use strict';
count = count - 1;
minutes = checklength(Math.floor(count / 60));
seconds = checklength(count - minutes * 60);
if (count < 0) {
clearInterval(counter);
return;
}
document.getElementById("timer").innerHTML = 'Next refresh in ' + minutes + ':' + seconds + ' ';
if (count === 0) {
location.reload();
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<span id="timer"></span>
Run Code Online (Sandbox Code Playgroud)