Author Topic: DHTML Clock  (Read 2114 times)

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
DHTML Clock
« on: February 24, 2007, 02:47:21 PM »
Here's a small code snippet of a DHTML clock.

Why? Just because I think every browser based game should display the time  --)

Here's how to do it (uses dynamic HTML):

in the <head></head> section:
Code: [Select]
<script language="JavaScript">
function Clock()
{
  if (!document.getElementById)
  theclock=document.getElementById("clock");
  now = new Date();
  hours=now.getHours();
  mins=now.getMinutes();
  secs=now.getSeconds();
  if (secs < 10) secs= "0" + secs;
  if (mins < 10) mins= "0" + mins;
  theclock.innerHTML = hours + ":" + mins + ":" + secs;
  window.setTimeout("Clock();",250); 
}
</script>

in the body section:
Code: [Select]
<body onLoad="Clock();">
<div id="clock">
00:00:00
</div>
</body>

Yeah, just wanted to contribute something today. --;

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: DHTML Clock
« Reply #1 on: February 25, 2007, 05:31:47 AM »
Hehe, thanks Sinzygy :)
Does this update the time, real-time? :P

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: DHTML Clock
« Reply #2 on: February 25, 2007, 05:56:12 AM »
As a matter of fact, it does  :D


With this you can implement a working (i.e. running in realtime) clock and customize how it looks.

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Need help with server-clock
« Reply #3 on: March 22, 2007, 03:18:04 PM »
Okay, I'm back again with something concerning clocks.

The clock I posted here only displays the time on the players computer.

But how would I go about displaying the server time?(by server time I mean the time the server is on)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: DHTML Clock
« Reply #4 on: March 22, 2007, 03:50:19 PM »
Well, if you have PHP, you could echo the time in seconds straight into the script. And if you're up for it, do some hardcore mathematics to calculate the seconds, minutes and hours :P Then, once you have the seconds, minutes and hour values in separate variables, you could update the seconds variable once every 1000ms, then the minutes variable once seconds hits 60, and so on.

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: DHTML Clock
« Reply #5 on: March 22, 2007, 04:25:55 PM »
Hmm... the pages are usually switched fast enough so that I could just output the time once and the don't have any real-time updates.

Lazy, yes; Simple, yes :D

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal