简体   繁体   English

如何停止setInterval()?

[英]How to stop the setInterval()?

below is the script of setInterval() and clearInterval() .. 下面是setInterval()clearInterval()的脚本。

I have searched for clearInterval() to stop the setInterval() ... I implemented that code, but it is not working.. 我已经搜索了clearInterval()来停止setInterval() …我实现了该代码,但无法正常工作。

What i am trying to achieve here is that, for example, there are 8 predator and 3 prey.. The setInterval will start when there is more Predators than Prey, so the Prey will reduce 3 while the Predator will increase 3 more.. And when the number of prey hit 0.. The predator should stop increasing since there is no more prey left.. 我要在这里实现的目标是,例如,有8个捕食者和3个捕食者。当捕食者多于捕食者时,setInterval将开始,因此,捕食者将减少3个,而捕食者将增加3个。当猎物的数量达到0时,捕食者应停止增加,因为没有更多的猎物了。

The problem here is that, when the prey hit 0, the predator is still increasing.. I added in clearInterval() but it is not working.. May i know what went wrong? 这里的问题是,当猎物命中0时,捕食者仍在增加..我在clearInterval()添加了它,但它不起作用..我可以知道出了什么问题吗?

<script>
    function startAni() {
        if ((document.getElementById('amount').value) >= (document.getElementById('amount5').value)) {
            alert("Predator is more than prey! FRENZY TIME!");
            var interval = setInterval(function () {
                Remove3(), removevalue3(), Add(), addvalues();
                if (document.getElementById('amount5').value = "0") {
                    alert("No more Prey!");
                    clearInterval(interval);
                }
            }, 5000);
        } else {
            alert("Prey is more than Predator! Revenge time!")
        }
    }
</script>

Many thanks in advance for helping! 在此先感谢您的帮助!

You need to ensure that there is an actual input on the DOM with the id amount5 because if there is then document.getElementById('amount5').value = "0" will be true and will clear your interval immediately ... 您需要确保DOM上的ID为amount5的实际input ,因为如果有,那么document.getElementById('amount5').value = "0"将为true ,并将立即清除您的间隔...

But

if (document.getElementById('amount5').value = "0") {

should be 应该

if (document.getElementById('amount5').value == "0") {

in javascript you use double equals ( == ) to compare. 在javascript中,您使用双等于( == )进行比较。

This: 这个:

if(document.getElementById('amount5').value = "0"){

You should use == instead of = , otherwise your condition is always truthy. 您应该使用==而不是= ,否则您的情况总是真实的。 Having said that, that statement would immediately kill the timer. 话虽如此,该声明将立即终止计时器。

Another way of looking at it is using setTimeout instead of setInterval and clearInterval : 另一种查看方法是使用setTimeout而不是setIntervalclearInterval

function doAdd() {
    Remove3(), removevalue3(), Add(), addvalues();
    if (document.getElementById('amount5').value = "0") {
        alert("No more Prey!");
        // thats it, no more recursion as your stop condition has been met
    }
    else {
        setTimeout(doAdd, 5000); // continue as there is more to be done
    }
}
setTimeout(doAdd, 0); // start it off

You made mistake on setting the interval variable scope. 您在设置间隔变量范围时出错。 You should define it as global variable. 您应该将其定义为全局变量。

If you define it in "startAni" function, the variable cannot be accessed by nested function in "startAni" function. 如果在“ startAni”函数中定义它,则该变量不能由“ startAni”函数中的嵌套函数访问。

You should define as: 您应该定义为:

<script>
    var interval;//define it here
    function startAni() {
        if ((document.getElementById('amount').value) >= (document.getElementById('amount5').value)) {
            alert("Predator is more than prey! FRENZY TIME!");
            interval = setInterval(function () {  //do not redefined interval as var just use it here
    ....

</script>

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

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