[英]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.