duanraotun1674 2016-10-20 14:13 采纳率: 100%
浏览 46
已采纳

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

I have used a countdown timer in my online examination website,the timer is shown via javaScript but problem is that after the 00:00:00 the timer shows negative time.I just want to stop the timer at 00:00:00 and submit a form when the time is over.below is the code that accurately displaying me the timer.

<?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条回答 默认 最新

  • douhuan1497 2016-10-20 14:17
    关注

    I assume that your form has an ID attribute with this value yourForm.

    // HTML
    <form id="yourForm">[...]</form>
    
    // JS
    if((hours==00)&&(mins==00)&&(secs==00)) {
      document.getElementById('yourForm').submit();
    } else {
      setTimeout(countdownto, 1000);
    }
    

    Note: Remove your setTimeout(countdownto, 1000); before the if statement

    Alternative way.

    You could kill the timeout with clearTimeout()

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

    clearTimeout documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记