Advanced Computer

Javascript

Assignment 4    -   Add a Clock

 

CLOCK.HTML

<html>

<head>

 

</head>

 

<body onload = "tick()">

 

<script type= "text/javascript" src="clock.js">

</script>

 

<form id = "f">

 

<h1>JavaScript time is now:</h1>

<input type="text" name="clock">

</form>

 

</body>

 

</html>

 

 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


CLOCK.JS

 

function tick()

{

 

                // create a new Date Object

                var now = new Date();

 

                // extract the current hours, minutes and seconds

                var hh = now.getHours();

                var mn = now.getMinutes();

                var ss = now.getSeconds();

 

                // ensure each components has two digits

                if( hh <= 9 ) hh = "0" + hh;

                if( mn <= 9 ) mn = "0" + mn;

                if( ss <= 9 ) ss = "0" + ss;

 

                // assign the current time string to the form field

                document.forms.f.clock.value = hh+ ": " +mn+ ": " +ss;

 

                // set the interval to one second

                window.setTimeout( "tick()", 1000 );

}