设置考试时间30分钟Javascript错误?

时间:2016-02-29 07:40:29

标签: javascript php

我正在为网络考试页面写作,在那里我必须设置30分钟到考试时间。所以我使用onloadsettimeout功能检查是否超过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>

3 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

您每次超时都会为m分配新值。你应该只初始化m一次。

同样minute来自0-59因此您无法检查它是否等于m + 30,这是错误的做法

据说在服务器端执行此超时总是更好,在客户端有足够的方法来欺骗它

答案 2 :(得分:0)

&#13;
&#13;
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);
}
&#13;
<body onload="time()">
  <!-- question code -->
<div id="time"></div><!-- show time  -->
</body>
&#13;
&#13;
&#13;