简体   繁体   中英

JS is ignoring my setInterval

I wrote a script to do some automated actions on a website (which is not mine).

That website is some kind of online shop for a pc game. The user selects an item and clicks a "withdraw" button. When the website is under heavy load (quite often), the user often gets a message saying "Heavy load - try again!" and has to click the same button again and again until he either gets the item or receives a message saying "The item was already sold!".

Everything is running inside a chrome extension!

My script does the following:

  • Add an onClick event to the button to run a function

  • Click "withdraw"

  • Read the message that comes from the website

  • Depends on the message:

    • "Offer is being sent..." - Do nothing and read again after interval

    • "Item was already sold!" - Stop the interval

    • "Heavy load - try again!" - Click an element to remove the message and "withdraw" again

The problem:

The interval is set to 2000ms, but the script just seems to be spamming the withdraw button nonstop and it seems to ignore the clearInterval().

My code:

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

Any help is appreciated!


Edit_1

Code right now:

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

}())

Thanks to Vitalii for this code - It seems to work better now since it's not constantly spamming the button anymore. Sadly, the other problem persists: If the script reaches for example this part of the code:

    if (message.includes("Trade offer has been sent!")) {
        console.log("Success: Stopping!")
        clearInterval(buyInterval);
    }

It successfully reads the message and prints out "Success: Stopping!" - once every two seconds ... ongoing until i stop it from doing that by hand. It seems like clearInterval(buyInterval); is still being ignored.

What am I doing wrong here?

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

}())

rewrite your forceBuy function like this -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM