...

View Full Version : stopwatch question



Wee Bubba
06-29-2004, 03:42 PM
i have a stopwatch which works:

my showtime function is working by using setTimeOut. i have been told that itis not that accurate and does not take into account the execution time for the code. What i actually want to do is save the start time and then take the difference between the two when i update the stopwatch.

my existing function is shown below. can anyone show me how to change this function to use the timing method i have described above? :confused:

showtime = function(){
seconds = seconds + 1
if (seconds == 60)
{
seconds = 0;
minutes = minutes + 1;
}
if (minutes == 60)
{
minutes = 0
hours = hours + 1
}
var timeValuehr = "" + ((hours < 10) ? "0" : "") + hours;
var timeValuemin = "" + ((minutes < 10) ? "0" : "") + minutes;
var timeValuesec = "" + ((seconds < 10) ? "0" : "") + seconds;
document.formTimeRec.elements['tbClock'].value = timeValuehr + " : " + timeValuemin + " : " + timeValuesec;
timerID = setTimeout("showtime()", 1000);
timerRunning = true;
}

neofibril
06-29-2004, 06:45 PM
I'm not sure about this, but...



var sTime;
function showtime()
{
if(!sTime)
sTime = new Date();
var nTime = new Date(new Date() - sTime);
nTime = ("0" + nTime.getUTCHours() + " : 0" + nTime.getMinutes() + " : 0" + nTime.getSeconds()).replace(/0(\d{2})/g, "$1");
document.formTimeRec["tbClock"].value = nTime;
setTimeout(showtime, 1000);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum