Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #12113
| From | Dr J R Stockton <reply1208@merlyn.demon.co.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: How to write a browser based timer |
| Date | 2012-02-23 19:31 +0000 |
| Organization | Home |
| Message-ID | <6Zcx$FIPQpRPFwYj@invalid.uk.co.demon.merlyn.invalid> (permalink) |
| References | <ji101v$3ra$1@dont-email.me> <a44b4295-4c38-40c3-bc86-f63bd24d7720@b23g2000yqn.googlegroups.com> <ht38k7pv0rr3vrinoj674dg70v5iic6tsq@4ax.com> <XnsA000F0C4BFBD3eejj99@194.109.133.133> <3bf207fd-3d2f-40ab-95e7-9a1ed5a2ffd2@vh10g2000pbc.googlegroups.com> |
In comp.lang.javascript message <3bf207fd-3d2f-40ab-95e7-9a1ed5a2ffd2@vh
10g2000pbc.googlegroups.com>, Tue, 21 Feb 2012 18:18:29, RobG
<rgqld@iinet.net.au> posted:
>
>If it is to update at a particular interval, the function should
>calculate the milliseconds to the next full intervale and call the
>function via setTimeout very soon after that (say 20 ms for a 1 second
>timer). So the function only needs to run once per scond for a timer
>with one second resolution.
>
>If the resolution is 0.1 seconds, then schedule it to run say 10ms
>after the next 100 ms interval so it runs about 10 times per second.
>If system load is heavy and it misses an interval, it will appear to
>skip but there is nothing you can do about that.
The answer, IMHO, is to use setInterval. I'm not sure what happens if
one calls for an interval of one second and, after a while, keeps the
CPU very busy for a few seconds - does one get a burst of late
setInterval handler calls, or are some lost? And what if, after
starting with setInterval, the machine is put into standby for a while?
But with setInterval, delays of substantially under a second in making
one handler call do not accumulate, as they would with simple repeated
setTimeout (Func, /const/)
To allow as best one can for such possibilities, the OP should NOT count
those handler calls for a measure of time, but should call new Date() to
get the time in milliseconds, from which simple arithmetic will get the
minutes, seconds, and maybe fractional part to display.
However, the valid objections to a wait loop along the lines of
while (new Date()<EndTime) { ... }
are fewer than commonly thought. A simple loop calculating the date of
Gregorian Easter Sunday for a full cycle or more takes a significant
while, but with at least some browsers the machine is still adequately
responsive.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to write a browser based timer " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2012-02-21 15:50 -0500
Re: How to write a browser based timer Scott Sauyet <scott.sauyet@gmail.com> - 2012-02-21 13:04 -0800
Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-21 13:45 -0800
Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-21 22:40 +0000
Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-21 15:04 -0800
Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-21 23:27 +0000
Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-21 16:08 -0800
Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-22 15:41 +0000
Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-22 09:19 -0800
Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-22 21:55 +0000
Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-22 15:01 -0800
Re: How to write a browser based timer RobG <rgqld@iinet.net.au> - 2012-02-21 18:18 -0800
Re: How to write a browser based timer Dr J R Stockton <reply1208@merlyn.demon.co.uk> - 2012-02-23 19:31 +0000
Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-23 16:38 -0800
Re: How to write a browser based timer Dr J R Stockton <reply1208@merlyn.demon.co.uk> - 2012-02-25 20:29 +0000
Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-26 23:02 +0000
Re: How to write a browser based timer Dr J R Stockton <jrs@merlyn.demon.co.uk> - 2012-02-22 18:51 +0000
Re: How to write a browser based timer " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2012-02-23 10:23 -0500
Re: How to write a browser based timer Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-23 22:13 +0100
Re: How to write a browser based timer Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-24 09:41 +0100
csiph-web