DOM之定时器计时效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input type="text" id="ip">
<button id="btn1">开始</button>
<button id="btn2">结束</button>
<script>
    var t;
    function showTime() {
        var ip = document.getElementById('ip');
        var time = new Date();
        ip.value = time.toLocaleString();
    }
    showTime();
    var btn1 = document.getElementById('btn1');
    btn1.onclick = function (e) {
        if (!t){
            t = setInterval(showTime,1000)
        };
    };
    var btn2 = document.getElementById('btn2');
    btn2.onclick = function (e) {
       clearInterval(t);
       t = undefined;
    };
</script>
</body>
</html>
posted @ 2021-09-02 18:02  拾呓  阅读(32)  评论(0编辑  收藏  举报