如何在计时器到达00:00:00时自动提交表单

时间:2016-10-20 14:13:20

标签: javascript php forms date timer

我在我的在线考试网站上使用倒数计时器,计时器是通过javaScript显示的,但问题是在00:00:00之后计时器显示负时间。我只想在00:00停止计时器: 00,并在时间结束时提交表格。下面是准确显示计时器的代码。

<?php
// Upon starting the section
session_start();    
$_SESSION['TIMER'] = time() + 600; // Give the user Ten minutes
?>  
    <script type="text/javascript">
var TimeLimit = new Date('<?php echo date('r', $_SESSION['TIMER']) ?>');

</script>


    <script type="text/javascript">
function countdownto() {
  var date = Math.round((TimeLimit-new Date())/1000);
  var hours = Math.floor(date/3600);
  date = date - (hours*3600);
  var mins = Math.floor(date/60);
  date = date - (mins*60);
  var secs = date;
  if (hours<10) hours = '0'+hours;
  if (mins<10) mins = '0'+mins;
  if (secs<10) secs = '0'+secs;
  document.body.innerHTML = hours+':'+mins+':'+secs;
  setTimeout("countdownto()",1000);

  if((hours==00)&&(mins==00)&&(secs==00))
      document.alert("time is over");
  }

countdownto();

</script>

2 个答案:

答案 0 :(得分:0)

我假设您的表单具有ID值属性yourForm

// HTML
<form id="yourForm">[...]</form>

// JS
if((hours==00)&&(mins==00)&&(secs==00)) {
  document.getElementById('yourForm').submit();
} else {
  setTimeout(countdownto, 1000);
}

注意:setTimeout(countdownto, 1000);声明之前删除if

替代方式。

您可以使用clearTimeout()

终止超时
var myTimeout = setTimeout(countdownto, 1000);

if((hours==00)&&(mins==00)&&(secs==00)) {
  clearTimeout(myTimeout);
  document.getElementById('yourForm').submit();
}

clearTimeout documentation

答案 1 :(得分:0)

让我们创建一个index.php文件,请检查它。在计时器上提交表单,您可以在javascript CountDown(5,div)中传递时间;功能。

        <html>
        <body>
          <form action="" method="post" name="mcQuestion" id="mcQuestion">
            Name:<input type="test" name="name" value="Test">
          <div><span id="display" style="color:#FF0000;font-size:15px"></span></div>
        <div><span id="submitted" style="color:#FF0000;font-size:15px"></span></div>
        </form>
        <script>
        var div = document.getElementById('display');
        var submitted = document.getElementById('submitted');

          function CountDown(duration, display) {

                    var timer = duration, minutes, seconds;

                  var interVal=  setInterval(function () {
                        minutes = parseInt(timer / 60, 10);
                        seconds = parseInt(timer % 60, 10);

                        minutes = minutes < 10 ? "0" + minutes : minutes;
                        seconds = seconds < 10 ? "0" + seconds : seconds;
                display.innerHTML ="<b>" + minutes + "m : " + seconds + "s" + "</b>";
                        if (timer > 0) {
                           --timer;
                        }else{
                   clearInterval(interVal)
                            SubmitFunction();
                         }

                   },1000);

            }

          function SubmitFunction(){

            submitted.innerHTML="Time is up!";
            document.getElementById('mcQuestion').submit();

           }
           CountDown(5,div);
        </script>

        </body>
        </html>