0

Im trying to create a small JavaScript timer in which a user has a limited amount of time to answer a question, and if the user does not answer in time, they will be directed back to the main page. All I get back from my code in terms of the timer is literally " [ ] ".

My code:

    <DOCTYPE! html>

<html>

<head>

<link rel="stylesheet" type="text/css" href="style_q1.css">

<script type="text/javascript">

var time="60";
var min="0";
var sec="0";

function startTimer() {
      min=parseInt(timer/60);
      sec=parseInt(timer%60);

      if(timer<1){

        window.location="index.html";

      }

      document.getElementById("time").innerHTML = "<b> Time Left: </b>"+min.toString()+":"+sec.toString();
      timer--;
      setTimeout(function(){
      startTimer();
      }, 1000) ;
}

</script>


</head>

<body onload="startTimer();">

  <div id="top">


  </div>


  <div id="logo">

<h1 style="color:white;"> Question 1 -  Geography </h1>

  </div>

  <div id="game_area">

 <center>  <h2> What is the capital of Ireland? </h2> </center>



  </div>


  <div id="time">

      <center> <b>[<span id="time" ></span></b>]</center>

  </div>

  </body>

  </html>
3
  • 2
    Why do you have 2 body tags? Commented Jan 17, 2018 at 18:54
  • 4
    You don't define timer anywhere. Commented Jan 17, 2018 at 18:55
  • I fixed the body FrankerZ Commented Jan 17, 2018 at 18:57

4 Answers 4

1

Just use setInterval(), it's pretty much designed for this:

UPDATE: Remember to stop the setinterval process when the time is up. Use this method clearInterval() to stop the process.

var secondsLeft = 60;

function startTimer() {
      var min=parseInt(secondsLeft/60);
      var sec=parseInt(secondsLeft%60);

      if(secondsLeft<1){
        alert('timer expired');

        //window.location="index.html";

      }

      document.getElementById("time").innerHTML = "<b> Time Left: </b>"+min.toString()+":"+sec.toString();
      secondsLeft--;
}

setInterval(startTimer, 1000);
<DOCTYPE! html>

<html>

<head>
<link rel="stylesheet" type="text/css" href="style_q1.css">
</head>

<body onload="startTimer();">

  <div id="top">


  </div>


  <div id="logo">

    <h1 style="color:white;"> Question 1 -  Geography </h1>

  </div>

  <div id="game_area">

 <center>  <h2> What is the capital of Ireland? </h2> </center>



  </div>
  <div id="time">

      <center> <b>[<span id="time" ></span></b>]</center>

  </div>

  </body>

  </html>

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

3 Comments

I think would be nice to mention that OP will need to stop the setInterval process using the method clearInterval.
Well, he wrote window.location = 'blah', so that timer is going to be done as soon as the page redirects.
Yes, but it's important to mention for future users using the same scenario with SPA. Of course, if you want :-)
0

A few things to note:

  • <!DOCTYPE> instead of <DOCTYPE!>
  • id attributes should be unique, you have two declared
  • <center> is deprecated. Find a solution using CSS.
  • Rather than adding an onload attribute to your <body> I would suggest adding an DOMContentLoaded event listener to your document.

    <link rel="stylesheet" type="text/css" href="style_q1.css">
    
    <script type="text/javascript">
    
        function startTimer() {
            min = parseInt(timer / 60);
            sec = parseInt(timer % 60);
    
            if (timer < 1) {
                window.location = "index.html";
            }
    
            document.getElementById("time").innerHTML = "<b> Time Left: </b>" + min.toString() + ":" + sec.toString();
            timer--;
            setTimeout(function () {
                startTimer();
            }, 1000);
        }
    
        var timer = 60,
            min = 0,
            sec = 0;
    
        document.addEventListener('DOMContentLoaded', function () {
            startTimer();
        });
    </script>
    

    Question 1 - Geography

    What is the capital of Ireland?

    []

Comments

0

spelling mistake

rename time to timer

<html>

<head>

<link rel="stylesheet" type="text/css" href="style_q1.css">

<script type="text/javascript">

var timer="60";
var min="0";
var sec="0";


function startTimer() {
      min=parseInt(timer/60);
      sec=parseInt(timer%60);

      if(timer<1){

        window.location="index.html";

      }

      document.getElementById("time").innerHTML = "<b> Time Left: </b>"+min.toString()+":"+sec.toString();
      timer--;
      setTimeout(function(){
      startTimer();
      }, 1000) ;
}

</script>


</head>

<body onload="startTimer();">

  <div id="top">


  </div>


  <div id="logo">

<h1 style="color:white;"> Question 1 -  Geography </h1>

  </div>

  <div id="game_area">

 <center>  <h2> What is the capital of Ireland? </h2> </center>



  </div>
  <body onload="startTimer();">

  <div id="time">

      <center> <b>[<span id="time" ></span></b>]</center>

  </div>

  </body>

  </html>

Comments

0
  • Your code has an error with the variable time
  • You can use setInterval to accomplish your scenario.
  • Remember to stop the setinterval process when the time is up.

var timer = 10;
var min = 0;
var sec = 0;
var refreshIntervalId;

function startTimer() {
      min=parseInt(timer / 60);
      sec=parseInt(timer % 60);

      if (timer < 1) {
        //window.location="index.html";
        console.log("Time is up!");
        
        clearInterval(refreshIntervalId);
        
        return;
      }

      document.getElementById("time").innerHTML = "<b> Time Left: </b>" + min + ":" + sec;
      
      timer--;
}

var refreshIntervalId = setInterval(startTimer, 1000);
<div id="top">
  </div>
  <div id="logo">
<h1 style="color:white;"> Question 1 -  Geography </h1>
  </div>
  <div id="game_area">
 <center>  <h2> What is the capital of Ireland? </h2> </center>
  </div>
  <div id="time">
  <center> <b>[<span id="time" ></span></b>]</center>
</div>  

Hope it helps!

Comments

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.