[英]How can I stop my Javascript code once an event handler has been clicked?
所以我的hmtl eventhandeling按鈕是
<input type="button" value="Stop" onclick="stop()">
而javascript函數stop()
為:
function stop()
{
stop.called = true;
}
我在這里調用此函數:
....
var interval = setInterval(function ()
{
// call generation again for each generation, passing in the previous generations best offspring
best_offspring = generation(best_offspring, characters, amount_offspring, mutation_rate, target_text);
document.getElementById("output_text").value = best_offspring;
document.getElementById("generations").value = generations;
if (score_met)
{
clearInterval(interval);
}
}, delay);
if (stop.called)
{
return;
}
// so that we know that the program has been run atleast once
start = true;
}
您是否需要更多代碼?
無論如何,我要在調用函數時停止執行。 但是,即使調用了stop()
函數,我的interval
循環仍會繼續。 我還嘗試過將“ if(stop.Called) inside
var interval`循環內。 但這也不起作用。
有任何想法嗎? 我需要提供更多信息嗎?
if (stop.called)
{
clearInterval(interval);
}
¿?
var interval = setInterval(function ()
{
// call generation again for each generation, passing in the previous generations best offspring
best_offspring = generation(best_offspring, characters, amount_offspring, mutation_rate, target_text);
document.getElementById("output_text").value = best_offspring;
document.getElementById("generations").value = generations;
if (score_met || stop.called)
{
clearInterval(interval);
}
}, delay);
if (stop.called)
{
clearInterval(interval);
return;
}
// so that we know that the program has been run atleast once
start = true;
}
SetInterval返回一個ID,因此您可以簡單地調用clearInterval(id);。 返回中斷等。不起作用,因為它實際上不是像for and while這樣的循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.