timer function

D

Darren Showers

I'm running a timer function that counts down from set time frames.
It has an odd tendency to drop 2 seconds at the beginning but then
work fine the rest of the way. Can anyone suggest what could be
happening here?


function startTimer() {
running = true;
var now = new Date();
start =now.getTime();
var seconds = getSec();
var setTime = 1000 * seconds;
endTime = start + setTime;
showCountDown()
}

function getSec() {
var r = document.frm.radio1;
for (var i=0; i<r.length; i++) {
if (!r.checked) continue;
else return r.value;
}
return null;
}

function showCountDown() {
var present = new Date();
moment = present.getTime();
if (endTime - moment<= 0) {
stopTimer();
displayTimer.innerHTML = "TIME'S UP!";
showTimeOver();
} else {
var lapse = new Date(moment - start);
var minLapse = lapse.getMinutes();
var secLapse = lapse.getSeconds() + 1;
var timeLapse = minLapse;
timeLapse += ((secLapse < 10) ? ":0" : ":") + secLapse;
document.frm.timeLapse.value = timeLapse;
var delta = new Date(endTime - moment);
var theMin = delta.getMinutes();
var theSec = delta.getSeconds();
theTime = theMin;
theTime += ((theSec < 10) ? ":0" : ":") + theSec;
document.frm.timerDisplay.value = theTime;
displayTimer.innerHTML = theTime;

if (running) {
timerID = setTimeout("showCountDown()",1000)
}
}
}

function stopTimer() {
displayTimer.innerHTML = theTime + " TIMER STOPPED";
clearTimeout(timerID);
running = false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top