简体   繁体   English

如何在 html div 的代码中显示 javascript 计时器?

[英]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.

相关问题 我该如何做一个JavaScript计时器,在计时器末尾显示div几秒钟并重复一次 - How can i do a javascript timer that show div at the end of the timer for few second and repeat it 我如何使用JavaScript在html中隐藏div来简化代码? - How do i simplify my code, hiding div's in html using javascript? 计时器为零时如何显示div? - How do I display a div when my timer hits zero? 如何将我的 html/javascript 代码保存为程序? - How do I save my html/javascript code as a program? 如何将我的 html 代码连接到 javascript 中的控制器和路由? - How do I connect my html code to controllers and routes in javascript? 使用JavaScript如何在我的页面中插入HTML代码? - Using JavaScript how do I insert html code into my page? 如何简化我的Javascript“显示/隐藏DIV”功能的代码? - How can I simplify my code for my Javascript “show/hide on DIV” function? 通过 javascript,如何将一个 div 添加到我的 html 脚本中,其中包含另一个 div? - Through javascript, how do I add a div to my html script with another div inside of it? 如何设置图像的计时器以javascript或html显示? - How do I set a timer for a image to appear in javascript or html? HTML如何使用javascript显示计时器? - HTML How do I display a timer using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM