Thomas Weiss
Thomas Weiss

Reputation: 375

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:

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?

Upvotes: 2

Views: 569

Answers (2)

Drag13
Drag13

Reputation: 5988

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

}())

Upvotes: 2

Jeetendra Chauhan
Jeetendra Chauhan

Reputation: 1977

rewrite your forceBuy function like this -

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

Upvotes: 1

Related Questions