在网页中显示运行的时间时钟
效果展示:

将代码添加到<head>js代码</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
h=checkTime(h)
m = checkTime(m)
s = checkTime(s)
document.getElementById('currentDate').innerHTML = h + ":" + m + ":" + s
t = setTimeout('startTime()', 500)
}
function checkTime(i) {
if (i < 10)
{ i = "0" + i }
return i
}
</script>
调用方法(显示位置):
<span id="currentDate"></span>
注意调用id和js中要一致.

浙公网安备 33010602011771号