我正在为网页写作,在那里我必须设置30分钟来检查time.So,我使用onload和settimeout函数检查是否超过30分钟,问题页关闭并进入完成页面。我想添加当前分钟30分钟,但它不工作,不要去finish.php!
<body onload="time()">
<!-- question code -->
<div id="time"></div><!-- show time -->
</body>JS
<script>
function time(){
var j = new Date();
var hr = j.getHours();
var sec = j.getSeconds();
var min = j.getMinutes();
var m = min + 30;//set 30 minutes exam times
if (m === min) {
location.href = "finish.php";
}
document.getElementById('time').innerHTML = hr + ":" + min + ":" + sec;
setTimeout(function() {
time()
}, 1000);
}
</script>发布于 2016-02-29 08:16:52
function time() {
var time = 60 * 1; //Replace 1 with 30 minutes
var minutes, seconds;
setInterval(function () {
minutes = parseInt(time / 60)
seconds = parseInt(time % 60);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
document.getElementById('time').innerHTML = minutes + ":" + seconds;
time--;
if (time < 0) {
location.href = "finish.php";
}
}, 1000);
}<body onload="time()">
<!-- question code -->
<div id="time"></div><!-- show time -->
</body>
发布于 2016-02-29 07:46:21
试试这个
function time(){
setTimeout(function() {
location.href = "finish.php";
}, 30*60*1000);
setInterval(function() {
var j = new Date();
var hr = j.getHours();
var min = j.getMinutes();
var sec = j.getSeconds();
document.getElementById('time').innerHTML = hr + ":" + min + ":" + sec;
}, 1000);
}发布于 2016-02-29 07:45:13
每次超时时,您都要为m分配新值。您应该初始化,m只需初始化一次。
另外,minute是从0-59开始的,所以您不能总是检查它是否等于m + 30,这是错误的做法
尽管如此,在服务器端执行这个超时总是更好,在客户端有足够的方法来欺骗它。
https://stackoverflow.com/questions/35694217
复制相似问题