1

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?

2

1 Answer 1

1

This code lets you to countdown from 5 to 1 and print into testTimer div.

<div id="testTimer"></div>

JS code

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. JS Feedle

Sign up to request clarification or add additional context in comments.

5 Comments

To add to this, You an find the breakdown of the time conversion of the countdown here w3Schools How TO - JavaScript Countdown Timer
Great - thanks guys. But how do I relate that to the existing code I have? In the existing code, the two things that it is doing are.. 1. take the time in milliseconds (that's set elsewhere and called in, so I don't have a choice of it being seconds, it WILL be milliseconds. 2. submit a form on countdown end. I can't see how to use the code Giorgi showed as part of my code - i.e. do both the submit and the show and do it from milliseconds.
Hi @JohnSpartacusScotcher, thank you for updating your request. I updated also the code and commented on the place where you can add some other functions too. Also, you can countdown from 10 to 0 now, where the last 0 digit will become red too. If this is the code you needed to get, please consider accepting.
That's great, Giorgi - thank you very much. I have it working now and counting down seconds. (I converted the milliseconds I have into seconds outside of the script)> The only last thing that would be great to understand how to do is how to show the timer in muites and seconds. E.g. 90 seconds would countdown from 1:30 (minutes and seconds) rather than 90. Thanks again. :-)
I generally use division or mod functions. try this out: stackoverflow.com/questions/21294302/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.