<script>
function tick() {
var hours, minutes, seconds, xfile
var intHours, intMinutes, intSeconds
var today
today = new Date()
intHours = today.getHours()
intMinutes = today.getMinutes()
intSeconds = today.getSeconds()
if (intHours == 0) {
hours = "12:"
xfile = "午夜"
} else if (intHours <12) {
hours = intHours+":"
xfile = "上午"
} else if (intHours == 12) {
hours = "12:"
xfile = "正午"
} else {
intHours = intHours - 12
hours = intHours + ":"
xfile = "下午"
}
if (intMinutes <10) {
minutes = "0"+intMinutes+":"
} else {
minutes = intMinutes+":"
}
if (intSeconds <10) {
seconds = "0"+intSeconds+" "
} else {
seconds = intSeconds+" "
}
timeString = xfile+hours+minutes+seconds
document.getElementById("t").innerHTML=timeString
window.setTimeout("tick()", 1000)
}
window.onload=tick
</script>
<div id="t"></div>
================================
Clock.innerHTML = timeString
定时重写clock的内容,达到时钟效果
js是单线程的,主线程之外有个任务队列用来放异步任务,定时器就是异步的操作,定时器设置的时间,是指在1000毫秒后,可以调用回调函数得到异步操作的结果,但是必须保证主线程中的同步任务已经全部执行完毕,如果主线程还有同步任务在执行,即使1000毫秒到了,也不会去读取异步的任务队列,就是说,这个定时器等待的最短时间是1000毫秒。<html><head>
<script type="text/javascript">
<!--
window.onload=function(){
var oDiv=document.getElementById('time') // 获取DIV
function theTime(){
var theDate=new Date() // 创建一个日期对象
var year=theDate.getFullYear() // 获取年份
var month=theDate.getMonth() // 获取月份
var day=theDate.getDate() //获取日
var hour=theDate.getHours() //获取小时
var minues=theDate.getMinutes() // 获取分钟
var second=theDate.getSeconds() // 获取秒
oDiv.innerHTML="现在的时间是"+year+"年"+month+"月"+day+"日 "+hour+":"+minues+":"+second
}
theTime() // 执行时间函数
setInterval(theTime,1000) // 更新时间
}
//-->
</script>
</head>
<div id="time"></div>
</html>
你试下,,,,