我正在做一个在线考试项目。我想在 10 分钟后提交页面,即使用户没有单击提交按钮。另外,我想显示时间倒计时。
最佳答案
这应该相当简单,只需使用 [setInterval][1]
进行倒计时并提交回发即可。
<div id="timer"></div>
<script type='text/javascript'>
var start = new Date();
setInterval(function() {
// check time elapsed given target time of 10 mins from now
var timeLeft = (600000 - (new Date() - start))/1000;
// update timer
document.getElementById('timer').innerHTML = Math.floor(timeLeft/60) + ":" + Math.floor(timeLeft%60);
// check if timer has elapsed, and submit form
if (timeLeft > 0) {
document.forms[0].submit();
}
}, 1000);
</script>
编辑根据评论,最好检查实际耗时,而不是依赖setInterval(这并不完全准确)。
关于c# - 网页倒计时器和自动提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12329013/