JavaScript timer code question! 500 POINTS!!

Hi,
I found the code below on the internet for a test timer.  I understand everything but the max time it allows is 60 minutes.  I have set it up so that after the first 60 minute session, another 60 minute session starts.  The code says that the max time is for 60 minutes.  I tried increasing the values so that the timer is for 120 minutes.  When I did that, the timer starts at 120 minutes and begins the countdown but then it starts jumping minutes sometimes by as much as 10 minutes.  Can someone please tell me how I can alter the code below so that the timer is for 120 minutes?

Thanks


function display(){
rtime=etime-ctime;
if (rtime>60)
m=parseInt(rtime/60);
else{
m=0;
}
s=parseInt(rtime-m*60);
if(s<10)
s="0"+s
window.status="Time Remaining (1st out of two 60 minute time period):  "+m+":"+s

window.setTimeout("checktime()",1000)
}


function display1(){
rtime=etime-ctime;
if (rtime>60)
m=parseInt(rtime/60);
else{
m=0;
}
s=parseInt(rtime-m*60);
if(s<10)
s="0"+s
window.status="Time Remaining (2nd out of two 60 minute time period):  "+m+":"+s

window.setTimeout("checktime1()",1000)
}




function settimes(){
alert("Test Format: 100 questions!  Timer can be seen at bottom-left of test page.")
alert("You have 2 hours time!  Timer will be split into two 60 minute sessions.")
alert("CLICK OK TO START TIMER!")

var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
etime=hours*3600+mins*60+secs;
etime+=3600;  //You can change the value of 1200 according to how much time you wish to set the timer. Where 1200 is time in secs (1200 = 20 mins * 60 secs/min). Max time is 60 mins (3600secs)
checktime();
}

function checktime(){
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
ctime=hours*3600+mins*60+secs
if(ctime>=etime){
settimes1();
}
else
display();
}


function checktime1(){
var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
ctime=hours*3600+mins*60+secs
if(ctime>=etime){
expired();
}
else
display1();
}



function settimes1(){
alert("You have 1 hour left!")

var time= new Date();
hours= time.getHours();
mins= time.getMinutes();
secs= time.getSeconds();
etime=hours*3600+mins*60+secs;
etime+=3600;  //You can change the value of 1200 according to how much time you wish to set the timer. Where 1200 is time in secs (1200 = 20 mins * 60 secs/min). Max time is 60 mins (3600secs)
checktime1();
}






function expired(){
alert("Time has expired!  Please click the Submit button now.  Delaying can void your test!");
//location.href="../main.html";  //Put here the next page
}



<BODY onLoad=settimes()....
NAPSRAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
sigmaconConnect With a Mentor Commented:
var timeLimit = 7200;            // time limit in seconds
var intervalRef = null;            // reference to the interval id
var startTime = null;

// add a leading 0 if necessary
function leadZero(x) {
      return (x < 0 || x > 9 ? '': '0') + x;
}

// format a time given in seconds to hours:minutes:seconds
function formatTime(aTime) {
      var hours = parseInt(aTime / 3600);
      var minutes = parseInt(aTime / 60) - hours * 60;
      var seconds = aTime - minutes * 60 - hours * 3600;
      return hours + ':' + leadZero(minutes) + ':' + leadZero(seconds);
}

function start() {
      alert('Click OK to start timer');
      startTime = new Date();
      intervalRef = setInterval('checkTimer()', 1000); // call every 1 second
}

function checkTimer() {
      var now = new Date();
      var timePast = now - startTime; // time past in milliseconds
      timePast = parseInt(timePast / 1000); // time past in seconds
      window.status = 'Time Remaining (hrs:min:sec): ' + formatTime(timeLimit - timePast);
      if (timePast >= timeLimit) {
            clearInterval(intervalRef);
            expire();
      }
}

function expire() {
      alert("Time has expired!  Please click the Submit button now.  Delaying can void your test!");
}
0
 
PerfunctionCommented:
Try setting up a variable that stores the timer length and replace every instance of 60 with that variable. Then, create another variable that stores the timer length seconds and replace every instance of 3600 with that variable.
0
 
NAPSRAuthor Commented:
Thanks for your input.

I need the max time to be 120 minutes or 7200 secs.  Do I just replace th 3600 secs above with the variable that holds the 7200 secs?

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
NAPSRAuthor Commented:
Do I need to change anything in the display function?
0
 
PerfunctionCommented:
Yeah, from the looks of things you need to replace all those hard-coded numbers with variables that can be used to control the timer. What I would do is setup on called timeMin and timeSec. Then you can set timeMin to 120 and set timeSec to timeMin * 60. I can't test the code here or I could help more
0
 
NAPSRAuthor Commented:
If its not too much trouble, can you copy and paste the code above with the suggested changes.  Thank you for your help.

0
 
PerfunctionCommented:
if you post the entire page's code i can play with it over here
0
 
sigmaconCommented:
This code is unnecessarily complex. The following code achieves essentially the same without the hack of using the same functions twice. All you need to do is change the timeLimit to whatever you need. I did not add any of the display code to format hours and minutes. I can do it if you can't do it yourself. Put a call to the start() method in your body onload:

var timeLimit = 7200;                  // time limit in seconds
var intervalRef = null;            // reference to the interval id
var startTime = null;

function start() {
      alert('Click OK to start timer');
      startTime = new Date();
      intervalRef = setInterval('checkTimer()', 1000); // call every 1 second
}

function checkTimer() {
      var now = new Date();
      var timePast = now - startTime; // time past in milliseconds
      timePast = parseInt(timePast / 1000); // time past in seconds
      window.status = 'Time Remaining: ' + (timeLimit - timePast) + ' seconds';
      if (timePast >= timeLimit) {
            clearInterval(intervalRef);
            expire();
      }
}

function expire() {
      alert("Time has expired!  Please click the Submit button now.  Delaying can void your test!");
}
0
 
NAPSRAuthor Commented:
Hi sigmacon,

Thank you very much for your help.  I tried the display code but I can't seem to get it right completely.  Can you please help me with that?

Thanks
0
 
sigmaconCommented:
NAPSR, how did this work out for you? Do you have any more questions?
0
 
NAPSRAuthor Commented:
Hi sigmacon,

Sorry I have not written in a while.  I was out of town and just returned.

The code is perfect!!  Thank you so much for helping me with it.  I have awarded you the points.  I will post any questions I might have about it here if needed.

Thanks
0
All Courses

From novice to tech pro — start learning today.