用JavaScript在网页上显示时间,并可暂停和开始

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a443512430/article/details/73472957

代码如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>时钟</title>
    <script type="text/javascript">
        function shijian(){
        var myclock = new Date();
        var year = myclock.getFullYear();
        var mon = myclock.getMonth()+1;
        var day = myclock.getDate();
        var HH = myclock.getHours();
        var mm = myclock.getMinutes();
        var ss = myclock.getSeconds();
        document.getElementById("shi").innerHTML=year+""+mon+""+day+""+"&nbsp;&nbsp;"+HH+":"+mm+":"+ss;

        }
        var myclock1 = setInterval("shijian()",1000);
        function stops(){
            clearInterval(myclock1);

        }
        function strat(){
            myclock1 = setInterval("shijian()",1000);
        }
    </script>
</head>
<body onload="shijian()">
<div id="shi"></div>
<input type="button" value="开始" onclick="strat();" style="width: 100px" >
<input type="button" value="暂停" onclick="stops();" style="width: 100px" >
</body>
</html>

页面显示如下

展开阅读全文

没有更多推荐了,返回首页