秒表设计-setInterval定时器的使用

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
<style>
    .clock{
        width:200px;
        height:50px;
        background: #000;
        color:#0f0;
        font-weight:bold;
        border-radius:50px;
        text-align:center;
        line-height:50px;
        font-size:30px;
        }
</style>
</head>
<body>
    <div class="clock">
        <span id="sid"></span>
    </div>
</body>

<script>
    function getDate(){
    //初始化
    dobj = new Date();

    //时、分、秒
    hour = dobj.getHours();
        if(hour<10){
            hour='0'+hour;
        }
    minute = dobj.getMinutes();
        if(minute<10){
            minute='0'+minute;
        }
    second = dobj.getSeconds();
        if(second<10){
            second='0'+second;
        }
        
    //弹出时间
    str =hour+':'+minute+':'+second;
    sidobj = document.getElementById('sid');
    sidobj.innerHTML=str;
    }

    //先调用一次,才不会出现刷新的时候时间延迟的现象
    getDate();

    //定时器执行
    setInterval(function(){
        getDate();
    },1000);

</script>

猜你喜欢

转载自blog.csdn.net/weixin_40205005/article/details/80349826