[英]JS is ignoring my setInterval
我编写了一个脚本来对网站(不是我的)执行一些自动操作。
该网站是某种PC游戏的在线商店。 用户选择一个项目,然后单击“撤回”按钮。 当网站负载沉重(经常)时,用户通常会收到一条消息,提示“负载沉重-再试一次!”。 并且必须一次又一次单击相同的按钮,直到他获得该物品或收到一条消息,指出“该物品已售出!”。
一切都在chrome扩展程序内运行!
我的脚本执行以下操作:
将onClick事件添加到按钮以运行功能
点击“提款”
阅读来自网站的消息
取决于消息:
“正在发送要价...”-不执行任何操作,间隔后再次阅读
“物品已经售出!” -停止间隔
“重负载-再试一次!” -单击元素以删除消息,然后再次“撤回”
问题:
间隔设置为2000ms,但是脚本似乎不停地向撤消按钮发送垃圾邮件,并且似乎忽略了clearInterval()。
我的代码:
function buy() {
//Get the innerHTML for the box that displays the message
var message = document.getElementsByClassName("pm-content")[0].innerHTML;
//Message: Offer is being sent! - DO NOTHING!
if (message == "Please wait while your trade offer is being sent...") {
console.log("Loading: Going on!")
}
//Message: Item is gone! - STOP EVERYTHING!
if (message == "Items unavailable") {
console.log("Unavailable: Stopping!")
clearInterval(buyInterval);
}
//Message: Transaction successfull! - STOP EVERYTHING
if (message.includes("Trade offer has been sent! Code: ")) {
console.log("Success: Stopping!")
clearInterval(buyInterval);
}
if (message == "Heavy load! - Try again!") {
console.log("Overload: Going on!")
document.getElementById("pgwModal").click(); //Click to remove the message
document.getElementById("withdraw").click(); //Click withdraw again
}
}
function forceBuy() {
buyInterval = setInterval(function(){ buy() }, 2000);
}
var button = document.getElementById("withdraw");
withdraw.onclick=function(){ forceBuy () };
任何帮助表示赞赏!
现在的代码:
(function(){ //creating isolated scope to avoid using global variables.
var buyInterval; // declaring sharing variables.
function buy() {
var message = document.getElementsByClassName("pm-content")[0].innerHTML;
if (message == "Please wait while your trade offer is being sent...<br><small>(this might take up to 5 minutes)</small>") {
console.log("Loading: Going on!")
}
if (message == "You cannot afford that withdrawal.") {
console.log("Broke: Stopping!")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}
if (message == "Items unavailable") {
console.log("Unavailable: Stopping!")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}
if (message.includes("Trade offer has been sent!")) {
console.log("Success: Stopping!")
clearInterval(buyInterval);
}
if (message.includes("missing")) {
console.log("Missing: Stopping")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}
if (message == "You can have only one pending deposit or withdrawal.") {
console.log("Pending: Stopping!")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}
if (message == "Too many active trades") {
console.log("Overload: Going on!")
document.getElementById("pgwModal").click();
document.getElementById("withdraw").click();
}
}
function forceBuy() {
return setInterval(function(){ buy(); }, 2000); // not necessary but // will be more readable
}
var button = document.getElementById("withdraw");
withdraw.onclick=function(){ //making a closure to catch buyInterval variable
buyInterval = forceBuy ();
};
}())
感谢Vitalii提供此代码-现在看来效果更好,因为它不再不断向按钮发送垃圾信息。 可悲的是,另一个问题仍然存在:例如,如果脚本到达了代码的这一部分,则:
if (message.includes("Trade offer has been sent!")) {
console.log("Success: Stopping!")
clearInterval(buyInterval);
}
它成功读取了该消息并打印出“成功:正在停止!”。 -每两秒钟一次...进行中,直到我停止手动执行此操作为止。 似乎clearInterval(buyInterval); 仍然被忽略。
我在这里做错了什么?
(function(){ //creating isolated scope to avoid using global variables.
var buyInterval; // declaring sharing variables.
function buy() {
... buying action
}
function forceBuy() {
return setInterval(function(){ buy(); }, 2000); // not necessary but // will be more readable
}
var button = document.getElementById("withdraw");
withdraw.onclick=function(){ //making a closure to catch buyInterval variable
buyInterval = forceBuy ();
};
}())
像这样重写您的forceBuy函数-
function forceBuy() {
if(buyInterval) clearInterval(buyInterval);
buyInterval = setInterval(function(){ buy() }, 2000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.