javascript定时器及Date对象

功能:显示时钟

<html>
<head>
<title>test</title>

</head>

<body onload="startTime();">
<div id="txt"></div>
</body>
</html>

<script type="text/javascript">
    function startTime() {
        var today = new Date()
        var y = today.getFullYear();
        var M = today.getMonth() + 1
        var d = today.getDate();
        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 = y + "-" + M + "-" + d + " " + h + ":" + m + ":" + s;
        t = setTimeout('startTime()', 500)    //定时器,500ms执行递归一次
    }

    function checkTime(i) {
        if (i < 10)
        { i = "0" + i }
        return i
    }
</script>

posted @ 2013-03-26 20:56  81  阅读(307)  评论(0编辑  收藏  举报