JS忽略了我的setInterval

时间:2016-06-10 08:32:05

标签: javascript google-chrome google-chrome-extension

我写了一个脚本来在网站上做一些自动化操作(这不是我的)。

该网站是某种PC游戏的在线商店。用户选择一个项目并单击“撤销”按钮。当网站负载很重(经常)时,用户经常收到一条消息“重载 - 再试一次!”并且必须一次又一次地点击相同的按钮,直到他得到该项目或收到一条消息“该项目已售出!”。

所有内容都在Chrome扩展程序中运行!

我的脚本执行以下操作:

  • 将onClick事件添加到按钮以运行功能

  • 点击“撤销”

  • 阅读网站上的消息

  • 取决于消息:

    • “正在发送优惠......” - 在区间

    • 之后不做任何事情并再次阅读
    • “物品已售出!” - 停止间隔

    • “重载 - 再试一次!” - 单击元素以删除消息并再次“撤消”

问题:

间隔设置为2000毫秒,但脚本似乎是不停地向撤销按钮发送垃圾邮件,它似乎忽略了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 () };

感谢任何帮助!

Edit_1

现在代码:

(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);仍被忽视。

我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

(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 ();
   };

}())

答案 1 :(得分:1)

像这样重写你的forceBuy函数 -

function forceBuy() {
    if(buyInterval) clearInterval(buyInterval);
    buyInterval = setInterval(function(){ buy() }, 2000);
}