在网页中显示运行的时间时钟


效果展示:

 

将代码添加到<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中要一致.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM