[英]Script execution doesn't stop when loop condition is false
Recently I wrote an userscript for auto betting on csgojustice. 最近,我写了一个用于自动下注csgojustice的用户脚本。 Here it is: 这里是:
var delay = 10000
var btBet = document.getElementById("make-bet");
var loop = true
var input=document.createElement("input");
input.type="button";
input.value="Start";
input.onclick = start;
input.setAttribute("style", "font-size:18px;position:absolute;top:120px;right:40px;");
document.body.appendChild(input);
var input=document.createElement("input");
input.type="button";
input.value="Stop";
input.onclick = stop;
input.setAttribute("style", "font-size:18px;position:absolute;top:120px;right:130px;");
document.body.appendChild(input);
function start() {
loop = true;
roll();
}
function roll() {
if (loop === true) {
btBet.click();
refreshIntervalId = setInterval(roll2, delay); }
}
function roll2() {
btBet.click();
clearInterval(refreshIntervalId)'
roll()
}
function stop() {
loop = false;
}
When I click Start button, it starts executing. 当我单击开始按钮时,它开始执行。 But when I click Stop button, it still executes. 但是当我单击“停止”按钮时,它仍然执行。 But there is if (loop === true)
yet when loop
is false
, it doesn't stop. 但是if (loop === true)
仍然存在, if (loop === true)
当loop
为false
,它不会停止。
I can't seem to experience your problem - this script works just fine for me. 我似乎无法遇到您的问题-该脚本对我来说很好用。
But I have fixed a few things. 但是我已经修复了一些问题。
roll2
function is not necessary. roll2
功能。 setTimeout
instead if setInterval
, then you don't have to clear it every time 如果使用setInterval
,则可以使用setTimeout
而不必每次都清除它 clearTimeout
when calling stop
你应该在调用stop
时clearTimeout
var delay = 1000;
var btBet = document.getElementById("make-bet");
var loop = true;
var input=document.createElement("input");
input.type="button";
input.value="Start";
input.onclick = start;
input.setAttribute("style", "font-size:18px;position:absolute;top:120px;right:40px;");
document.body.appendChild(input);
var input=document.createElement("input");
input.type="button";
input.value="Stop";
input.onclick = stop;
input.setAttribute("style", "font-size:18px;position:absolute;top:120px;right:130px;");
document.body.appendChild(input);
function start() {
loop = true;
roll();
}
function roll() {
if (loop === true) {
console.log("roll");
refreshIntervalId = setTimeout(roll, delay);
}
}
function stop() {
loop = false;
clearTimeout(refreshIntervalId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.