©ilj@flowsim.se
JavaScript code samples 63
JavaScript clock - display current time using dynamic images
>>> this example in Swedish



 




 
Here´s the code:

<SCRIPT LANGUAGE="JavaScript"
      TYPE="text/javascript">
<!--
function gettime()
{
      var datum = new Date()
//
      iljhour=datum.getHours()
//
      iljmin=datum.getMinutes()
//
      iljsec=datum.getSeconds()
//
      if(iljmin<10){iljmin="0"+iljmin}
//
      if(iljhour<10){iljhour="0"+iljhour}
//
      if(iljsec<10){iljsec="0"+iljsec}
//
      iljhour=iljhour+""
//
      iljsec=iljsec+""
//
      iljmin=iljmin+""
//
}
if(document.images)
{
       gettime()
      iljdone=0
       bild=new Array()
       for(i=0;i<10;i++)
       {
      bild[i]=new Image()
       }
       for(i=0;i<10;i++)
       {
      bild[i].src=i+".gif"
       }
}

function showtime()
{
if((document.images)&&(iljdone<600))
      {
       iljtimer=setTimeout("showtime()",1000)
      gettime()
      var startpict=0
      document.images[startpict+0].src=bild[iljhour.charAt(0)].src
      document.images[startpict+1].src=bild[iljhour.charAt(1)].src
      document.images[startpict+2].src="kolon.gif"
      document.images[startpict+3].src=bild[iljmin.charAt(0)].src
      document.images[startpict+4].src=bild[iljmin.charAt(1)].src
      document.images[startpict+5].src="kolon.gif"
      document.images[startpict+6].src=bild[iljsec.charAt(0)].src
      document.images[startpict+7].src=bild[iljsec.charAt(1)].src
      iljdone++
       }
}
//-->
</SCRIPT>
 
The code above is placed in the HEAD portion of the document.
 
The code assumes that the
IMG tags of the clock are the first IMG tags on the page; if they are not, the number after startpict has got to be changed. If there is one picture before the clock the number should be changed to 1, if there are two pictures before the clock the number should be changed to 2 etc
 
The code also assumes that the 10 pictures used are named
0.gif, 1.gif, 2.gif etc, that the colon is represented by a picture named kolon.gif and that the image files and the HTML file is in the same folder or directory.
 

 

 

 
Replace the numbers after
WIDTH and HEIGHT in the code below by the WIDTH and HEIGHT of your pictures; this part of the code should be placed where the clock is to be displayed.
 

<SCRIPT LANGUAGE="JavaScript"
      TYPE="text/javascript">
<!--
if(document.images&&iljdone<600)
      {
      document.write("<IMG SRC='")
      document.write(bild[iljhour.charAt(0)].src+"'")
      document.write("HEIGHT='12' WIDTH='6'>")
      document.write("<IMG SRC='")
      document.write(bild[iljhour.charAt(1)].src+"'")
      document.write("HEIGHT='12' WIDTH='6'>")
      document.write("<IMG SRC='kolon.gif'")
      document.write("HEIGHT='12' WIDTH='6'>")
      document.write("<IMG SRC='")
      document.write(bild[iljmin.charAt(0)].src+"'")
      document.write("HEIGHT='12' WIDTH='6'>")
      document.write("<IMG SRC='")
      document.write(bild[iljmin.charAt(1)].src+"'")
      document.write("HEIGHT='12' WIDTH='6'>")
      document.write("<IMG SRC='kolon.gif'")
      document.write("HEIGHT='12' WIDTH='6'>")
      document.write("<IMG SRC='")
      document.write(bild[iljsec.charAt(0)].src+"'")
      document.write("HEIGHT='12' WIDTH='6'>")
      document.write("<IMG SRC='")
      document.write(bild[iljsec.charAt(1)].src+"'")
      document.write("HEIGHT='12' WIDTH='6'>")
}
//-->
</SCRIPT>
 
Add a call (that will start the clock) after BODY ONLOAD, like this:
 
<BODY ONLOAD="showtime()">
 

 

 

 
You can copy the code and use it almost as it is - but do not forget to
  • change the WIDTH and the HEIGHT!
  • perhaps change the number after startpict
  • perhaps change the number 600, on the line if((document.images)&&(iljdone<600)); this line determines for how long (how many seconds) the clock is working

 

 


OTHER JAVASCRiPT EXAMPLES
 
How to use javascript to choose what image to show by selecting it in an option list
How to use javascript to switch images
How to use javascript to switch images onmouseover
JavaScript getDay() and document.write
JavaScript date
JavaScript timer
How to use javascript to switch images onmouseover
JavaScript timer
JavaScript onmouseover
JavaScript lastModified
JavaScript/ text in text area
JavaScript/ text in text area
how to open new windows in JavaScript
JavaScript clock
JavaScript confirm
JavaScript alert
JavaScript prompt
JavaScript onmouseover
JavaScript onmouseover2
JavaScript animation
JavaScript animation 2
 

Page by
 
ilj
 
ilj@flowsim.se
 

 
Last modified: February 23, 1999
 
This page should be part of a frames system
at
http://www.flowsim.se