Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created a quiz in PHP. Each user can select a subject and attempt a quiz.
I wish to have a timer for 2 minutes for the quiz and after two minutes.It should display time over.Problem is that the timer itself is not loading.
Below is my code.

C#
<script>
var today1=new Date();
var h1=today1.getHours();
var m1=today1.getMinutes();
var s1=today1.getSeconds();

function startTime()
{
var today2=new Date();
var h2=today2.getHours();
var m2=today2.getMinutes();
var s2=today2.getSeconds();

// add a zero in front of numbers<10
h=checkTime(abs(h2-h1));
m=checkTime(abs(m2-m1));
s=checkTime(abs(s2-s1));
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
if(m==2)
{

    alert("Time Over");
    return;
}

t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>



I have included these so that timer should start
XML
<body onload = 'startTime()';>
<div id="txt"></div>


But the timer is not loaded...
Posted

1 solution

Use setInterval(). Adapt the following example to your need.
XML
<!DOCTYPE html>
<html>
<body onload="starttimer()">
<div id="txt"></div>
<script>
function starttimer() {
    var secondHolder = document.getElementById('txt');

    var secondsRemaining = 10; // Count down from 10 seconds

    var interval = setInterval(function() {
        secondHolder.innerHTML = --secondsRemaining;

        if (secondsRemaining <= 0)
        {
            secondHolder.innerHTML = 'Time Out!';
            clearInterval(interval);
        }
    }, 1000); // display remaining seconds every second
}
</script>
</body>
</html>

Reference: http://www.w3schools.com/jsref/met_win_setinterval.asp[^]
 
Share this answer
 
v3
Comments
sudarshan25 7-Jun-15 1:44am    
@Peter Leow Thanks for this. I have customized this to my script which is mentioned below.
<script>
function starttimer() {
var secondHolder = document.getElementById('txt');

var secondsRemaining = 2*60; // Count down from 10 seconds
var minutes =secondsRemaining/60;
var seconds =0;
secondsRemaining=secondsRemaining -1;
secondHolder.innerHTML =checktime(minutes)+":"+checktime(seconds);

seconds =59;
minutes=minutes -1;
var interval = setInterval(function() {



secondHolder.innerHTML =checktime(minutes)+":"+checktime(seconds);
seconds =seconds -1;
if(seconds == 0)
{
seconds = 59;
minutes = minutes -1;
}
secondsRemaining=secondsRemaining-1;

if (secondsRemaining <= 0)
{
//secondHolder.innerHTML = 'Time Out!';
alert("Timeout");
clearInterval(interval);


}
}, 1000); // display remaining seconds every second
}
function checktime(i)
{


if(i<=9)
{
i="0"+i;
}

return i;
}

But I wish to have a timer which starts when a session begins and ends when session ends.
The timer should not restart on page refresh , any help would be useful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900