/    Sign up×
Community /Pin to ProfileBookmark

setTimeOut problem

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?

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;
}

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJun 29.2004 — Why don't you just create a new [url=http://www.w3schools.com/js/js_obj_date.asp]date object[/url], and have your setTimeout() update the time every half a second or so? Since you're actually using the client's clock it you don't have to worry about the execution time.
Copy linkTweet thisAlerts:
@Wee_BubbaauthorJun 30.2004 — hello. sorry to bother you again and thanks for your reply. is this what you mean please? or can you see any flaws in this new code? many thanks for your expertise.

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);

}
Copy linkTweet thisAlerts:
@ExuroJul 01.2004 — Yes, that works, but you might want to turn down the timeout to half a second or so, to increase accuracy...
Copy linkTweet thisAlerts:
@Wee_BubbaauthorJul 03.2004 — thanks mate. set it down to 300 and its working just fine now.
×

Success!

Help @Wee_Bubba spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...