[英]How do I show the javascript timer in my code in an html div?
I have a simple timer that times a test, which is working great我有一个简单的计时器来计时测试,效果很好
$(document).ready(function() {
var varTimerInMiliseconds = 3000;
setTimeout(function(){
document.getElementById("cntnt01moduleform_1").submit();
}, varTimerInMiliseconds);
});
However, I also want to show the countdown to the student who is taking the test.但是,我也想向正在参加考试的学生展示倒计时。 How can I pass the timer itself into a div with the ID of id="testTimer"
and how can I convert the timer into minutes and seconds rather than milliseconds?如何将计时器本身传递到 ID 为id="testTimer"
的 div 中,以及如何将计时器转换为分钟和秒而不是毫秒?
This code lets you to countdown from 5 to 1 and print into testTimer
div.此代码可让您从 5 到 1 倒计时并打印到testTimer
div。
<div id="testTimer"></div>
JS code JS代码
function Timer() {
var counter = 10;
var myTimer = setInterval(function() {
document.getElementById("testTimer").innerHTML = counter;
counter--;
if (counter < 0) {
clearInterval(myTimer);
document.getElementById("testTimer").style.color = "red";
// do anything then time is up. ex: submit() function
document.getElementById("cntnt01moduleform_1").submit();
}
}, 1000);
}
Timer();
As you can see there is no need for translating milliseconds to seconds by multiplying.如您所见,无需通过乘法将毫秒转换为秒。 You just do the update of seconds once in 1000 ms.您只需每 1000 毫秒更新一次秒。 JS Feedle JS馈线
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.