Introduction to JavaScript

Digital Clock

<SCRIPT>

var colon = '<IMG SRC="digits/colon.gif">';

document.write('<IMG SRC="digits/0.gif" NAME="h0">');
document.write('<IMG SRC="digits/0.gif" NAME="h1">');
document.write(colon);
document.write('<IMG SRC="digits/0.gif" NAME="m0">');
document.write('<IMG SRC="digits/0.gif" NAME="m1">');
document.write(colon);
document.write('<IMG SRC="digits/0.gif" NAME="s0">');
document.write('<IMG SRC="digits/0.gif" NAME="s1">');
document.write('<IMG SRC="digits/am.gif" NAME="tod">');

// create the "digits" array, the array will hold the location of all the gif digits
var digit = new Array();

// use the digit Array elements to create the digit Image Objects
// use the for loop to fill up the array elements with the digit image locations

for (i = 0; i < 10; i++) {
     digit[i] = new Image();
     digit[i].
src = "digits/" + i + ".gif";
}

// this function swaps out the respective digits with the appropriate digits
function
upDate() {
     var now = new Date();

     var ampm = (now.getHours() >= 12) ? " pm" : " am";

     // if time.getMinutes() equals 35 then
     // Math.floor(35 / 10) == 3 for the
1st minute digit
     // 35 % 10 = 5 for the
2nd minute digit
     document.images['h0'].src = digit[Math.
floor(now.getHours() / 10)].src
     document.images['h1'].src = digit[now.
getHours() % 10].src
     document.images['m0'].src = digit[Math.
floor(now.getMinutes() / 10)].src
     document.images['m1'].src = digit[now.
getMinutes() % 10].src
     document.images['s0'].src = digit[Math.
floor(now.getSeconds() / 10)].src
     document.images['s1'].src = digit[now.
getSeconds() % 10].src ;
     document.images['tod'].src = "digits/" + ampm + ".gif";

     // wait 1 second then call the updateClockImage() function
    
setTimeout("upDate()", 1000);
}

</SCRIPT>

<BODY onLoad="upDate();">