简体   繁体   English

为什么我的setInterval循环在退出前又要经过一轮,即使我一开始就终止了

[英]Why does my setInterval loop go another round before exiting even though I have the termination in the beginning

Sorry for gore title.. But if you take a look at this code 对不起,标题。但是如果您看一下这段代码

var interval = setInterval(function () {
            if (start == false || score_met == true) {
                clearInterval(interval);
            }

            // call generation again for each generation, passing in the previous generations best offspring
            best_offspring = generation(best_offspring, characters, target_text, mutation_rate, amount_offspring);
            document.getElementById("output_text").value = best_offspring;
            document.getElementById("generations").value = generations;
            }, delay);
    });

This will loop untill the score is met or if I change the value of start . 这将循环直到达到分数或如果我更改start的值。 My reset button code looks like this 我的重置按钮代码如下所示

        //resets
    $( "#reset" ).click(function() {
            start = false;
            score_met = false;
            generations = 0;
            best_offspring = "";

            document.getElementById("start_text").value = "";
            document.getElementById("characters").value = " abcdefghijklmnopqrstuvwxyz";
            document.getElementById("target_text").value = "methinks it is like a weasel";
            document.getElementById("output_text").value = "";
            document.getElementById("mutation_rate").value = 5;
            document.getElementById("amount_offspring").value = 100;
            document.getElementById("delay").value = 50;
        });
    });

So when I click reset my loop should end right? 因此,当我单击“重置”时,循环应该正确结束吗? But it seems like it does another loop before it terminates. 但是似乎它在终止之前执行了另一个循环。 Why is that? 这是为什么? I was thinking that you could do something like 我以为你可以做类似的事情

$( interval ).ready(function() {...});

but then Im quite unsure what to put instead of interval there. 但是我非常不确定该放什么,而不是在那里放间隔。

Explaining your code. 解释您的代码。

var interval = setInterval(function () {
        if (start == false || score_met == true) {
            clearInterval(interval);
        }

        // call generation again for each generation, passing in the previous generations best offspring
        best_offspring = generation(best_offspring, characters, target_text, mutation_rate, amount_offspring);
        document.getElementById("output_text").value = best_offspring;
        document.getElementById("generations").value = generations;
        }, delay);

You are clearing the interval that is fine, But this part of code has already started executing , so the part of the code after if will execute at this moment . 您正在清除间隔不错,但是this part of code has already started executing ,因此the part of the code after if will execute at this moment So put it inside a else and you must be fine. 因此,将其放在另一个中,您一定会没事的。

var interval = setInterval(function () {
        if (start == false || score_met == true) {
            clearInterval(interval);
        }
        else{
          // call generation again for each generation, passing in the previous generations best offspring
          // your stuff here
         }           
        }, delay);

Simply do clearInterval(interval); return; 只需做clearInterval(interval); return; clearInterval(interval); return; to exit the function after clearing. 清除后退出功能。 Clearing prevents the next call, it will still continue the current call until it terminates: 清除会阻止下一个呼叫,它将继续当前呼叫直到终止:

var interval = setInterval(function () {
    if (start == false || score_met == true) {
        clearInterval(interval);
        return;
    }
    /* ... */
}, 1000);

You could also wrap the rest of your code in an else , which would also skip the rest of the code and terminate the function naturally. 您还可以将其余代码包装在else ,这也将跳过其余代码并自然终止函数。

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

相关问题 为什么我的 for 循环在我的条件之前退出 - Why is my for loop exiting before my condition 为什么我的循环没有停止,即使它已经结束了? - Why does my loop not stop, even though it's at the end? 即使我在后台运行了模拟器,也无法运行我的React本机代码 - I can't run my react native code even though I have an emulator running in the back round 为什么循环下面的语句在循环终止之前执行 - why does statement below loop executes before termination of loop 为什么我的元素有一个滚动条,即使它适合它的容器? - Why does my element have a scrollbar even though it fits its container? 为什么即使我之前返回,第二个函数声明也会获胜? - Why does second function declaration win even though I return before it? 对于循环退出之前,我希望它 - For loop exiting before I want it to 即使导入了我的函数,该函数也无法在另一个函数范围之外工作 - My function not working outside of another function scope even though I have imported it 为什么即使将window.onload放在HTML后面,我的JS也只能正常工作? - Why is my JS only working if I put it after the HTML, even though I have a window.onload? 为什么即使我已将数字转换回字符串,我也不能将数字推送到我的字符串数组中? - Why can I not push a number into my array of strings even though I have converted the number back to a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM