The best way to update a JavaScript clock.

D

Daz

Hi everyone.

I have a relatively simple question which is hindered only by my lack
of understanding.

I am creating a clock (as if that's never been done before...), and I
was stuck as to whether I should use setTimeout() or setInterval().
One of the advantages I can see with using setInterval(), is that if
JavaScript is disabled, and then re-enabled, the setInterval() resumes
from where it left off. Can anyone confirm this for other browsers?

Anyhow... My main question, is that I don't know whether I can safely
use setTimeout() or setInterval(), at one second intervals safely, or
whether I am likely to lose any time over prolonged periods. For
example:

Would:
setInterval(function(){someFunctionThatGetsTheNumberOfSeconds();},
1000);
work the same as
setTimeout(function(){someFunctionThatGetsTheNumberOfSeconds();},
1000); ?

or would I be better of using something like:

setInterval(function(){someFunctionThatGetsTheNumberOfSeconds();},
200);
or
setTimeout(function(){someFunctionThatGetsTheNumberOfSeconds();},
200); ?

Obviously, the latter 2 are more CPU intensive, but is it overkill?
Could I run a reliable timer (in seconds), using 1 second intervals,
or should I check the seconds elapsed using the Date() object more
frequently?

I hope this make sense, as it's quite hard for me to word.

Many thanks in advance.

Daz.
 
M

mouseit101

Hi everyone.

I have a relatively simple question which is hindered only by my lack
of understanding.

I am creating a clock (as if that's never been done before...), and I
was stuck as to whether I should use setTimeout() or setInterval().
One of the advantages I can see with using setInterval(), is that if
JavaScript is disabled, and then re-enabled, the setInterval() resumes
from where it left off. Can anyone confirm this for other browsers?

Anyhow... My main question, is that I don't know whether I can safely
use setTimeout() or setInterval(), at one second intervals safely, or
whether I am likely to lose any time over prolonged periods. For
example:

Would:
setInterval(function(){someFunctionThatGetsTheNumberOfSeconds();},
1000);
work the same as
setTimeout(function(){someFunctionThatGetsTheNumberOfSeconds();},
1000); ?

or would I be better of using something like:

setInterval(function(){someFunctionThatGetsTheNumberOfSeconds();},
200);
or
setTimeout(function(){someFunctionThatGetsTheNumberOfSeconds();},
200); ?

Obviously, the latter 2 are more CPU intensive, but is it overkill?
Could I run a reliable timer (in seconds), using 1 second intervals,
or should I check the seconds elapsed using the Date() object more
frequently?

I hope this make sense, as it's quite hard for me to word.

Many thanks in advance.

Daz.

The easiest way would be to find the time using the Date object every
second through a clock update call, I don't think setInterval or
setTimeout can be depended upon for much accuracy.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
(e-mail address removed) wrote on 27 mei 2007 in comp.lang.javascript:
Hi everyone.

I have a relatively simple question which is hindered only by my lack
of understanding.

I am creating a clock (as if that's never been done before...),
[...]
The easiest way would be to find the time using the Date object every
second through a clock update call, I don't think setInterval or
setTimeout can be depended upon for much accuracy.

<http://www.merlyn.demon.co.uk/js-clndr.htm#ck>

& <http://www.merlyn.demon.co.uk/js-dates.htm>.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top