473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

The best way to update a JavaScript clock.

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(){someFunctionThatGetsTheNumb erOfSeconds();},
1000);
work the same as
setTimeout(function(){someFunctionThatGetsTheNumbe rOfSeconds();},
1000); ?

or would I be better of using something like:

setInterval(function(){someFunctionThatGetsTheNumb erOfSeconds();},
200);
or
setTimeout(function(){someFunctionThatGetsTheNumbe rOfSeconds();},
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.

May 26 '07 #1
3 1775
On May 26, 4:59 pm, Daz <cutenfu...@gmail.comwrote:
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(){someFunctionThatGetsTheNumb erOfSeconds();},
1000);
work the same as
setTimeout(function(){someFunctionThatGetsTheNumbe rOfSeconds();},
1000); ?

or would I be better of using something like:

setInterval(function(){someFunctionThatGetsTheNumb erOfSeconds();},
200);
or
setTimeout(function(){someFunctionThatGetsTheNumbe rOfSeconds();},
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.

May 26 '07 #2
mo********@gmail.com wrote on 27 mei 2007 in comp.lang.javascript:
On May 26, 4:59 pm, Daz <cutenfu...@gmail.comwrote:
>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>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 27 '07 #3
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Sun, 27 May 2007 08:31:32, Evertjan. <ex**************@interxnl.net>
posted:
>mo********@gmail.com wrote on 27 mei 2007 in comp.lang.javascript:
>On May 26, 4:59 pm, Daz <cutenfu...@gmail.comwrote:
>>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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
May 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Valiz | last post by:
Hi, I am updating system time with IRIG time source which sends irregular pulses as shown below 11000011111100111111111111111111....(1-IRIG present and 0-IRIG not present) I need to update...
8
by: Prometheus Research | last post by:
http://newyork.craigslist.org/eng/34043771.html We need a JavaScript component which will auto-submit a form after a set period has elapsed. The component must display a counter that dynamically...
9
by: ME | last post by:
can someone tell me how to make this first one work??, located here: http://javascript.about.com/library/scripts/blshowdatetime.htm#examplesource This second one, well, dreamweaver eats the code,...
0
by: Kamyk | last post by:
Hello all! I have problem with code. Firstly I have created the VBScript code which count me the time and days which left to the end of working week. I work from 8:00 am till 4 pm. I would like...
4
by: Reney | last post by:
I have a very weird problem in updating my datagrid. Please help me to solve it. The datagrid is tied to a dataset table with five columns. Three of them are primary key and the other two columns...
7
by: ian ward | last post by:
Hello, I want to show the result of some JavaScript on an HTML page. I've had a look at some threads and it seems the conclusion is the following - javascript entities are usable on the...
1
by: cancer2006 | last post by:
I have to correct the ID field in 19 tables and there are 265 records affected in just one table . The id field is populated on the values from EInfo.ID, and EInfo.ID is based on the values from...
12
by: cenktarhancenk | last post by:
is there a way to display a ticking clock in a web page using javascript? but not in a textbox, rather as text that i can change the style, font etc. cenk tarhan
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.