Scripting: javascript clock

started by Mmiv on Feb 3, 2004 — RSS Feed

Mmiv Mmiv
Posts: 4

Hello to all!

as you will see soon Iím giving the first steps in the world of web design.

Iím sorry if the question is basic but I really need to learn how to do this.

can somebody help me with this script:

<Script Language="javascript">

     dt = new Date()
     var stemp;
     var stamp;

     var dt_hour = dt.getUTCHours() ;

     var dt_minute = dt.getUTCMinutes() ;

     var dt_second = dt.getUTCSeconds() ;


     if (dt_minute < 10)

           dt_minute = '0' + dt_minute ;


     if (dt_second < 10)

           dt_second = '0' + dt_second ;

     stemp = dt_hour+ ":" + dt_minute + ":" + dt_second ;
   stamp =dt_hour + 7 + ":" + dt_minute + ":" + dt_second ;
     sta=dt_hour + 7
     if (sta > 24)
     stamp= dt_hour - 17 + ":" + dt_minute + ":" + dt_second ;

document.write("<center>"+"<b>"+"<font color=#0099CC size=2>"

if ((ste > 7) && (ste <19))
document.write("<img src='sol.gif'>"+"<br>"
document.write("<img src= 'lua.gif'>"+"<br>"
document.write("Hora de Lisboa  "+stemp+"<br>"

if ((sta > 7) && (sta <19))
document.write("<img src= 'sol.gif'>"+"<br>"
document.write("<img src= 'lua.gif'>"+"<br>"
document.write("Hora de Jacarta  "+stamp+"<br>"


this script probably will shock some of you. I know... I know.. but as I told Iím giving the fist steps

The question: the clocks doesn't move and i don't know where to use the setTimout()

can some body help me please?


Joe Gillespie Joe Gillespie
Posts: 528

The problem with this script, even it did update, is that it would keep drawing the clocks down the page with every document.write.

This script updates the time in a form text area.

<script language="JavaScript">

// please keep these lines on when you copy the source
// made by: Nicolas -

var clockID = 0;

function UpdateClock() {
  if(clockID) {
     clockID  = 0;

  var tDate = new Date();

  document.theClock.theTime.value = ""
                                  + tDate.getHours() + ":"
                                  + tDate.getMinutes() + ":"
                                  + tDate.getSeconds();
  clockID = setTimeout("UpdateClock()", 1000);
function StartClock() {
  clockID = setTimeout("UpdateClock()", 500);

function KillClock() {
  if(clockID) {
     clockID  = 0;


<body onload="StartClock()" onunload="KillClock()">
<center><form name="theClock">
<input type=text name="theTime" size=8>

it wouldn't be too difficult to add another time zone.

Mmiv Mmiv
Posts: 4

Thanks a million, I will try!

By the way congratulations for this excellent page and forum

this kinds of things make me a believer in the human race again.  

Mmiv Mmiv
Posts: 4

thanks again i solved my problem!

Jdenny Jdenny
Posts: 65

If you ever put a clock on a web page, then don't make it change every second - it would be very distracting. So take out the seconds, and also then you only need to update the clock each minute not every second...

Instead work out the time left untill the current minute is up, then setTimeout() for that, at whcih point you update the clock text, and set a new time-out for 1 minute.

Maskodok Eko Maskodok Eko
Posts: 171

Jasa SEO Profesional

TOP SEO | Jasa SEO Profesional Agen Texas Poker Dan Domino Online Indonesia Terpercaya IDRPoker.COM Agen Texas Poker dan Domino Online Indonesia Terpercaya Agen Texas Poker Dan Domino Online Indonesia Terpercaya D

You must login to reply