Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22216

Re: OT: Displaying current time on a website

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: OT: Displaying current time on a website
Date 2013-11-29 14:39 +0100
Organization albasani.net
Message-ID <l7a5en$870$1@news.albasani.net> (permalink)
References <l70gbn$b7j$1@dont-email.me> <l75ncg$qc2$1@dont-email.me> <l78evn$7jc$1@news.albasani.net> <l78jc8$o49$2@softins.softins.co.uk> <l7a3rn$7d6$1@softins.softins.co.uk>

Show all headers | View raw


On 2013-11-29 14:12, Tony Mountifield wrote:
>         var clienttime = new Date().getTime();
>         var servertime = serverstart + (clienttime-clientstart);
...
>         setTimeout(update,1000-(servertime%1000));

Yes, this is pretty much what I had in mind. It could be simplified a
little, by combining `serverstart` and `clientstart` into a fixed
`clientoffset`, or by using `+new Date()`, but the principle remains the
same. If updating the display takes a "long" time (>1ms), it might be
better to call setTimeout() before displaying the new time.

I think the important points in this exercise are

- use setTimeout() instead of setInterval()
- read the current client time on every update
- calculate the number of ms left until the next tick, and use that
  value in setTimeout()

Nicely done.

- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Re: OT: Displaying current time on a website Rhino <no_offline_contact_please@example.com> - 2013-11-27 16:15 -0500
  Re: OT: Displaying current time on a website Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-28 01:11 +0000
    Re: OT: Displaying current time on a website "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-11-28 11:20 +0200
      Re: OT: Displaying current time on a website Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-29 00:12 +0000
        Re: OT: Displaying current time on a website Andrew Poulos <ap_prog@hotmail.com> - 2013-11-29 13:51 +1100
        Re: OT: Displaying current time on a website Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-29 10:58 +0100
    Re: OT: Displaying current time on a website Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-11-29 21:30 +0000
  Re: OT: Displaying current time on a website Stefan Weiss <krewecherl@gmail.com> - 2013-11-28 23:09 +0100
    Re: OT: Displaying current time on a website tony@mountifield.org (Tony Mountifield) - 2013-11-28 23:24 +0000
      Re: OT: Displaying current time on a website tony@mountifield.org (Tony Mountifield) - 2013-11-29 13:12 +0000
        Re: OT: Displaying current time on a website Stefan Weiss <krewecherl@gmail.com> - 2013-11-29 14:39 +0100
        Re: OT: Displaying current time on a website Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-11-30 20:35 +0000
          Re: OT: Displaying current time on a website "Evertjan." <exjxw.hannivoort@interxnl.net> - 2013-12-01 07:57 +0000

csiph-web