0

So in the past couple of days I have decided to learn a bit more about JavaScript and as a lesson to myself I have decided to pursue a project and learn things along the way. I decided to try to make a kind of "bot" for a game. I am trying to write out some code and then just paste it into the HTML console( game is in a web browser). In this game there is an enemy and to kill it you click the button "attack" repeatedly until it dies. Once it dies you are presented with a screen that has a button "Back to Patrolling" which allows you to go back and choose another enemy. What I currently have is

var BTP = document.getElementsByClassName('Patrol-button-name');

checkPatrol(BTP);

function checkPatrol(LEN) {
if (LEN.length > 0) {
document.getElementsByClassName("Patrol-button name")[0].click()}
else {
clickAttack();}
}

function clickAttack() {
    return this.document.getElementsByClassName("Attack-Button")[0].click()
    return this.checkPatrol(BTP);
}

Basically, checkPatrol uses length to determine if the Patrol button has appeared on the screen. If it has appeared it clicks it however if it hasn't appeared then it calls the clickAttack function which returns the click of the attack button. This segment works but what my question is, is why doesn't it loop again and again until the patrol button is clicked? the clickAttack function chains back into the checkPatrol function so why doesn't it keep looping until the enemy is killed and the patrol button is clicked? When I test this it does the attack once although it seems to me my code is telling it to loop? Why isn't it looping and what am I doing wrong? Could someone then outline a correct way to loop these two functions? Thanks to whoever actually took the time to read this :P.

  • Remove the `return` statements from your `clickAttack` function. You're not using them, and the first one is causing the `checkPatrol` function call to never happen. – 4castle Oct 28 '17 at 21:57
  • 1
    Possible duplicate of [Does return stop a loop?](https://stackoverflow.com/questions/11714503/does-return-stop-a-loop) – 4castle Oct 28 '17 at 22:01
  • So should I keep everything the same but just remove return this? Will the attack function execute both the click and the call of chackPatrol? – Jayson Okhman Oct 28 '17 at 22:29
  • Right, you only need to remove the `return` from the front of each statement. I should have been clearer. – 4castle Oct 28 '17 at 22:30
  • Thanks! It seems to be looping but now I get a million errors and the page just goes white. Is this because I need to add some delays into it? – Jayson Okhman Oct 28 '17 at 22:44
  • I can't tell you for sure, because you're the one with the code, but yes, adding some calls to `setTimeout` will delay your code enough to keep it from looping into oblivion. – 4castle Oct 28 '17 at 22:47
  • Alright so I have implemented setTimeout by doing setTimeout(clickAttack(), 3000)} but im still getting the maximum call stack error. Is this because of a fundamental problem in my code or because im using the timeout incorrectly? Does the timeout help prevent the maximum call error? Or do I need to stop looping functions into themselves? – Jayson Okhman Oct 28 '17 at 23:52
  • `setTimeout` takes a function reference as the first parameter, not a function call. Use `setTimeout(clickAttack, 3000);` – 4castle Oct 29 '17 at 01:28
  • Thanks a lot 4castle! Completed my goal after 2 hours of researching and rewriting. I Deleted the previous one you see above and made a nice simplified version which works beautifully. Thanks for the advice and help! – Jayson Okhman Oct 29 '17 at 03:46

0 Answers0