实时显示时间

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
    <title>时钟特效</title>
    <script type="text/javascript">

        function disptime() {
            var today = new Date(); //获得当前时间
            var hh = today.getHours();  //获得小时、分钟、秒
            var mm = today.getMinutes();
            var ss = today.getSeconds();

            document.getElementById("myclock").innerHTML = "<h1>现在是:" + hh + ":" + mm + ":" + ss + "<h1>";
            /*
             使用setTimeout在函数disptime()体内再次调用setTimeout
             设置定时器每隔1秒(1000毫秒),调用函数disptime()执行,刷新时钟显示
             var myTime=setTimeout("disptime()",1000);
             */
        }
        /*使用setInterval()每间隔指定毫秒后调用disptime()*/
        var myTime = setInterval("disptime()", 1000);

    </script>
</head>

<body>
<div id="myclock"></div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/danqiu2017/article/details/79229283
今日推荐