python定時器運用(實現動態實時顯示)


計時相關:
    1.指定時間之后做一件事
        setTimeout(js語句,毫秒)
    2.每隔一段時間做一件事
        setInterval(js語句,毫秒)
        clearInterval(setInterval的變量名)清除重復事件

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="content-Type" charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <title>Title</title>
</head>
<body>

<input type="text" id="i1">
<button id="b1">開始</button>
<button id="b2">停止</button>
<script>
    var i1Ele = document.getElementById("i1");
    var t;

    function f() {
        var now = new Date();
        i1Ele.value = now.toLocaleString();
    }


    f();
    var b1Ele = document.getElementById("b1");
    // 點開始
    b1Ele.onclick = function (ev) {
        if (!t) {
            t = setInterval(f, 1000)
        }
    };

    var b2Ele = document.getElementById("b2");
    // 點停止
    b2Ele.onclick = function (ev) {
        clearInterval(t);  // 根據id清除定時任務
        console.log(t);
        t = null;
    }
</script>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM