【问题标题】:Is there a way to display a message once a timer has reached 0 and then restart the timer?有没有办法在计时器达到 0 后显示消息然后重新启动计时器?
【发布时间】:2011-07-02 12:52:22
【问题描述】:

我想在 javascript 计时器内显示一条消息,一旦它达到 0 并重新启动。 我的计时器代码和我的其他类似问题尚未得到解答:

How do you display a message once a javascript function restarts?

【问题讨论】:

    标签: javascript javascript-framework timertask


    【解决方案1】:

    试试这个。

    你错过了window.clearInterval的正确实现

    var c = 10;
    var t;
    
    function timedCount() {
        document.getElementById('txt').value = c;
        c = c - 1;
        if (c == 0){
            clearTimeout(t);
            //alert something here
            c = 10;
            doMining();
        }
    
    }
    
    function doMining() {
        t = setInterval(function () {
            timedCount();
        }, 1000);
    }
    

    window.setInterval的签名如下。

    var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
    var intervalID = window.setInterval(code, delay);
    

    【讨论】:

    • 你能解释一下下面这行是如何工作的吗? t = setInterval(function () { timedCount(); }, 1000);
    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多