<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()//得到当前时间
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()//得到时分秒
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)//0.5秒后执行 加上上次执行的0.5秒 就是隔一秒执行一次了
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}//给分钟,秒钟不满10的数字前面加上0
</script>
</head>
<body οnlοad="startTime()">
<div id="txt"></div>
</body>
</html>
使用计时事件制作钟表
<html>
<body>
<script type="text/javascript">
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname
this.lastname=lastname
this.age=age
this.eyecolor=eyecolor
}
myFather=new person("John","Adams",35,"black")
document.write(myFather.firstname + " 的年龄是 " + myFather.age + " 岁。")
</script>
</body>
</html>
创建用于对象的模板