简体   繁体   English

计时器达到0时如何触发功能?

[英]How to fire a function when timer reaches 0?

I'm currently working on a short quiz game but I cant seem to figure it out. 我目前正在做一个简短的问答游戏,但似乎无法弄清楚。 I feel like I have everything that I need, but I may have possibly placed them in a wrong order? 我觉得自己拥有了所需的一切,但可能可能是因为顺序错误?

JS: JS:

function stopTimer() {
    clearInterval(intervalID);
}

function startTimer() {
    intervalID = window.setInterval(function() {
        animations()
    }, 1000);
    time = 30;
}

animations = function() {
    time--;
    if (time >= 0) {
        timer.textContent = time;
    } else if (time == 0) {
        EndToAlert(); //*This is the function im trying to fire once timer 
        reaches 0 * //
            stopTimer();
    }
}

使用if (time == 0)而不是else if (time == 0) ,因为当time = 0时,将执行上面的if (time >= 0) ,但是else if (time == 0)不会执行。

只是将else如果(time == 0)更改为else。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM