Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Server Side Running Clock (Servlets & Javascript)

15 views
Skip to first unread message

Nino Skilj

unread,
May 10, 2003, 12:41:11 AM5/10/03
to
I had looked around for quite some time on the groups for a clock that
would display the server time to the user, and update real time. I was
only able to find slight leads, and I was able to piece together a
piece of code that did what I wanted. I thought I would take the time
to share my findings for anyone else struggling through this...

The Object:
Get the server time to show up on the clients browser and update
real time

The Dilemma:
Javascript is a client side scripting language, so the clocks you
see all over the net won't work.

The Solution:
Use Java Servlets (or any server side language) to call the server
time, initialize the javascript server clock, and then use the client
side javascript to update the clock while keeping track of the time
manually.

Code:

import java.util.Date;
.....
Date date = new Date();
.....
out.println(" <SCRIPT type=\"text/javascript\"
language=\"javascript\">");
out.println(" var timer = null");
out.println(" var crnttime = 0");
out.println(" var mstime = 0");
out.println("");
out.println(" function initializeClock() {");
//Initialize the clock using the server time
out.println(" mstime = "+date.getTime()+"");
out.println(" crnttime = new Date("+date.getTime()+")");
out.println(" startClock()");
out.println(" }");
out.println("");
out.println(" function stopClock() {");
out.println(" clearTimeout(timer)");
out.println(" }");
out.println("");
out.println(" function startClock() {");
out.println(" var hours = crnttime.getHours()");
out.println("");
out.println(" var minutes = crnttime.getMinutes()");
out.println(" minutes=((minutes < 10) ? \"0\" : \"\") +
minutes");
out.println("");
out.println(" var seconds = crnttime.getSeconds()");
out.println(" seconds=((seconds < 10) ? \"0\" : \"\") +
seconds");
out.println("");
out.println(" var clock = hours + \":\" + minutes + \":\" +
seconds");
out.println(" document.forms[0].display.value = clock");
out.println("");
out.println(" mstime += 1000");
out.println(" crnttime = new Date(mstime)");
out.println(" timer = setTimeout(\"startClock()\",1000)");
out.println(" }");
out.println(" </SCRIPT>");
out.println("</HEAD>");
out.println("<BODY onload=\"initializeClock()\"
onunload=\"stopClock()\">");
....
//Put this where you want your clock to be...
out.println(" <input type=\"text\" name=\"display\"
size=\"20\"></TD>");

Conclusion:
I hope this helps someone out. It still needs some work (adjusting
for time zones, etc), but its a start! Feel free to email me with any
questions/comments/error reporting/etc...

nino
nino9...@yahoo.com

Ingo Pakleppa

unread,
May 10, 2003, 3:15:35 AM5/10/03
to
This is a javascript question rather than a Java question.

You can probably get the server's time from one of the HTTP headers
without explicitly requesting it.

If you don't mind the traffic, you could also request the actual current
time from the server once per second.

rf

unread,
May 10, 2003, 7:29:28 AM5/10/03
to

"Nino Skilj" <nino9...@yahoo.com> wrote in message
news:b5f5e117.03050...@posting.google.com...

> I had looked around for quite some time on the groups for a clock that
> would display the server time to the user, and update real time.

Er, why would I need a clock set to your server timezone? As I am probably
on the other side of the planet from you your time is, to me, irrelevant.

Cheers
Richard.


Andrew Thompson

unread,
May 10, 2003, 9:52:38 AM5/10/03
to
"Ingo Pakleppa" <ingo-ne...@kkeane.com> wrote in message
news:pan.2003.05.10....@kkeane.com...

> This is a javascript question rather than a Java question.

Actually, it's not. If you look carefully at the 4Kb of text
you failed to trim in your reply (tut, tut), you will see that
the problem was _solved_ with Java.

--
Andrew Thompson
http://physci.org/


Nino Skilj

unread,
May 12, 2003, 3:46:35 PM5/12/03
to
"rf" <making...@the.time> wrote in message news:<so5va.1000$DP4....@news-server.bigpond.net.au>...

> Er, why would I need a clock set to your server timezone? As I am probably
> on the other side of the planet from you your time is, to me, irrelevant.

i didnt feel it was relevant to bring up my need to the problem, but
ill go ahead and explain... i am building a registration piece for
people that are given a time to register. since everyone will be
registering through one server (located locally for all users), the
best way to do it was to compare their time to the servers, and grant
them access accordingly. well, i thought it would be nice for people
to know what the "current time" was, and they can compare it to their
registration time. since everything would be done locally, then the
server time is relevant...

why cant people in these newsgroups just help others solve the
questions they ask as opposed to questioning their use for it? the
time issue was obviously something i felt i needed... why isnt that
enough?

nino

Evertjan.

unread,
May 12, 2003, 5:06:13 PM5/12/03
to
Nino Skilj wrote on 10 mei 2003 in comp.lang.javascript:
> I had looked around for quite some time on the groups for a clock that
> would display the server time to the user, and update real time. I was
> only able to find slight leads, and I was able to piece together a
> piece of code that did what I wanted. I thought I would take the time
> to share my findings for anyone else struggling through this...

Using ASP the problem is trivial:

============= test.asp ==================

<div id=div1>.</div>

<script runat=server language=javascript>
ds = new Date();
ds=ds*1;
</script>

<script>
ds= <% =ds %>; // UTC server
dc=new Date();
dc=dc*1; // UTC client
dif = dc-ds; // UTC difference
showtime();

function showtime(){
d=new Date();
document.getElementById("div1").innerHTML=
"Client time:<br>"+ d + "<br><br>";
dd=new Date(d*1+dif);
document.getElementById("div1").innerHTML+=
"Server corrected time:<br>"+ dd;
setTimeout("showtime()",1000);
};
</script>

========================================

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan.

unread,
May 12, 2003, 5:13:56 PM5/12/03
to
Evertjan. wrote on 12 mei 2003 in comp.lang.javascript:
> Using ASP the problem is trivial:
>
> ============= test.asp ==================
>
> <div id=div1>.</div>
>
> <script runat=server language=javascript>
> ds = new Date();
> ds=ds*1;
> </script>
>
> <script>
> ds= <% =ds %>; // UTC server
> dc=new Date();
> dc=dc*1; // UTC client
> dif = dc-ds; // UTC difference
> showtime();
>
> function showtime(){
> d=new Date();
> document.getElementById("div1").innerHTML=
> "Client time:<br>"+ d + "<br><br>";
> dd=new Date(d*1+dif);
> document.getElementById("div1").innerHTML+=
> "Server corrected time:<br>"+ dd;
> setTimeout("showtime()",1000);
>};
> </script>
>
> ========================================
>

not so trivial apparently, it should be:

dif = ds-dc; // UTC difference

rf

unread,
May 12, 2003, 11:25:09 PM5/12/03
to

"Nino Skilj" <nino9...@yahoo.com> wrote in message
news:b5f5e117.03051...@posting.google.com...


Why can't people who post questions to these newsgroups provide all the
relevant information right up front, in the original post. These newsgroups
are about the web, not just a local area. Anything discussed here is
assumed to be world wide (thats the first two w's in www) and do to with the
internet, unless specifically indicated otherwise.

Now that I see exactly where you are coming from I see my answer should be
changed.

Why do you feel the need to put yet another clock on your viewers screen.
Don't you think they can just look at their system tray, or their wrist, or
the wall?

Cheers
Richard.


Andrew Thompson

unread,
May 13, 2003, 3:47:50 AM5/13/03
to
x-posted group c.l.js dropped..

"rf" <making...@the.time> wrote in message

news:pAZva.3974$DP4....@news-server.bigpond.net.au...


>
> "Nino Skilj" <nino9...@yahoo.com> wrote in message
> news:b5f5e117.03051...@posting.google.com...
> > "rf" <making...@the.time> wrote in message
> news:<so5va.1000$DP4....@news-server.bigpond.net.au>...
> >
> > > Er, why would I need a clock set to your server timezone?

I wondered the same myself, but..

> > i didnt feel it was relevant to bring up my need to the problem,

...


> Why can't people who post questions to these newsgroups provide all the
> relevant information right up front, in the original post.

Aaah. I suffer this 'what is relevant' problem as well.

In this case, I considered asking Nino why?
But, having never needed to show time on
web pages, I just shrugged and moved on.
__
But when I'm posting, I always need to
make similar decisions about what is
relevant and what is not.

A concise 2K post can turn into a long,
boring 10K nobody reads, very quickly.
__
It seems this one can be put down to a
difference of opinion on how much was
relevant..

:-)

0 new messages