【问题标题】:Countdown timer automaticaly trigger the Submit Button倒数计时器自动触发提交按钮
【发布时间】:2020-10-06 10:21:29
【问题描述】:

我正在制作一个关于 PHP 的在线考试项目。

我希望倒数计时器在超时时自动触发提交按钮?

如果可能,如果计时器达到 1 分钟标记,则在计时器下方显示另一个 div 标签。

function startTimer(duration, display) {
  var timer = duration,
    minutes, seconds;
  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.textContent = minutes + ":" + seconds;

    if (--timer < 0) {
      timer = duration;
    }
  }, 1000);
}

window.onload = function() {
  var examTime = 60 * 5,
    display = document.querySelector('#time');
  startTimer(examTime, display);
};
#timediv {
  background-color: #003300;
  color: #ffffff;
  font-size: 20px;
  text-align: center;
}
<div id="timediv">Exam ends in <span id="time">05:00</span> minutes!</div>
<form action="samepage.php" method="post">
  <input type="text" />
  <input type="submit" value="Submit Answer Paper" />
</form>

【问题讨论】:

  • 如果计时器达到 1 分钟标记,则在计时器下方显示另一个这是什么意思?
  • 同一个计时器可以用来指示我认为还剩 1 分钟的时间?!问题出在哪里?我在计时器函数中没有看到对表单的引用或尝试提交它 - 这是问题吗?
  • 我没有看到任何“php”。那么,这真的是一个与 php 相关的问题吗?如果有,怎么做?

标签: javascript redirect time timer countdowntimer


【解决方案1】:

我希望以下内容相当容易理解。对于" if possible, display another below the timer if the timer reaches 1 minute mark.",这个问题有点模糊,所以我将其解释为在考试还剩 1 分钟时向用户“提供警告或某种形式”。

当倒数计时器到达其指定时间时,将调用提交表单的命令 - 该方法只是 Form.submit() 其中Form 是对表单的引用,以任何适合的方式派生。

仅供参考 - 计时器最初已修改为 1 分钟多一点,因此无需等待即可看到效果,等待再等待......

另外——在这里的代码中,我使用alert 来表示时间不多了……最好在屏幕上显示一条消息!

function startTimer(duration, display) {
	var timer = duration;
	var minutes, seconds;
	var warning = 60;
	var ok=false;
	var form=document.forms.exam;
	
	var t=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.textContent = minutes + ":" + seconds;
		
		if( timer <= warning && !ok ){
			alert('Time is running out.... less than 1 minute to go!');
			ok=true;
		}

		if (--timer <= 0) {
			timer = duration;

			if( !isNaN( t ) )clearInterval( t );
			
			// submit the form...
			form.submit();
		}
	}, 1000);
}

window.onload = function() {
	var examTime = 60 * 1.1;
	var display = document.querySelector('#time');
	startTimer(examTime, display);
};
<div id='timediv'>Exam ends in <span id='time'>05:00</span> minutes!</div>
<form name='exam' action='samepage.php' method='post'>
  <input type='text' />
  <input type='submit' value='Submit Answer Paper' />
</form>

【讨论】:

    【解决方案2】:

    您可以将此逻辑添加到您的 startTimer 函数中:

    // ...
    if (--timer < 0) {
        document.getElementById("theForm").submit();
        // ...
    }
    // ...
    

    您必须将&lt;form&gt; 上的id 属性设置为theForm

    【讨论】:

      【解决方案3】:

      setInterval回调里面,检查时间是否到了0:

      function startTimer(duration, display) {
        var timer = duration, minutes, seconds;
        setInterval(function() {
          minutes = parseInt(timer / 60, 10)
          seconds = parseInt(timer % 60, 10);
      
          if (minutes === 0 && seconds === 0) {
            document.querySelector('form').submit()
          }
      
          minutes = minutes < 10 ? "0" + minutes : minutes;
          seconds = seconds < 10 ? "0" + seconds : seconds;
      
          display.textContent = minutes + ":" + seconds;
      
          if (--timer < 0) {
            timer = duration;
          }
        }, 1000);
      }'
      

      您可以以相同的方式显示消息,但检查分钟是否等于一。

      if (minutes === 1 && seconds === 0) {
        // Display an alert or another timer 
      }
      

      【讨论】:

        猜你喜欢
        • 2015-10-17
        • 1970-01-01
        • 1970-01-01
        • 2014-08-17
        • 1970-01-01
        • 2015-12-20
        • 2021-08-27
        • 1970-01-01
        • 2012-04-18
        相关资源
        最近更新 更多