Search code examples
javascriptwhile-loopsettimeoutdelay

trying to delay using setTimeout() in a while loop


basically im trying to make an automation script for a gambling website . if you know roulette game mode you would now that you have about 20 seconds to choose a color and 10 seconds for the outcome to appear each round . what im trying to do is making a while loop for the script to run until the balance reaches a certain amount . what im struggling with is using the set timeout function to make a delay every 30 seconds so the betting bot can bet each round basically it waits 30 seconds then the bot bets . example :

do{
    delay 30 seconds amount 
    run bot()
}while(condition)

when i tried to use the set timeout function and used the script on the website its started betting infinitely and the whole thing crashed because the delay didn't happen correctly. this is the code :

function Bot(bt, bc)
{

  l = ((document.getElementsByClassName("roulette-past-queue--previous-rolls-container horizontal-scroll")[0].innerText).length);
  f = document.getElementsByClassName("roulette-past-queue--previous-rolls-container horizontal-scroll")[0].innerText;
  number = f[l - 1];
  number = parseInt(number.replace(" ", ""));
  if (number >= 1 && number <= 7)
  {
    LastResult = "red";
  }
  if (number >= 8 && number <= 14)
  {
    LastResult = "black";
  }
  if (number == 0)
  {
    LastResult = "green";
  }
  if (LastResult == bc)
  {
    if (bc == "red")
    {
      document.getElementById('bet-input-r').value = bt;
      document.getElementById('roulette-btn-red').click();
    }
    else
    {
      document.getElementById('bet-input-r').value = bt;
      document.getElementById('roulette-btn-black').click();
    }

  }
  if (LastResult != bc)
  {
    if (bc == "red")
    {
      document.getElementById('bet-btn-double-r').click();
      document.getElementById('roulette-btn-black').click();
      bc = "black"
        bt = bt * 2

    }
    else
    {
      document.getElementById('bet-btn-double-r').click();
      document.getElementById('roulette-btn-red').click();
      bc = "red"
        bt = bt * 2
    }
  }

}

balance = document.getElementById('balance').innerHTML;
balance = parseInt(balance.replace(",", ""));
bet = 10
  TargetBalance = 99988
  document.getElementById('bet-input-r').value = bet;
betcolor = "black";
if (betcolor == "red")
{
  document.getElementById('roulette-btn-red').click();
}
else
{
  document.getElementById('roulette-btn-black').click();
}
//this is the looping part causing the problem i have deleted setTimout so you can see what i mean clearly
do
{
  // 30 seconds delay
  Bot(bet, betcolor)

}
while (balance < TargetBalance && balance > bet)

Solution

  • This would be the simplest way, but you would have to remove the loop, otherwise, you will get gazzilion calls. Add this check at the end of your Bot function.

      if(balance < TargetBalance && balance > bet) {
          setTimeout(() => {
              Bot(bet, betcolor)
          }, 30000); // Expects ms, so 1000 * 30
      }
    

    There is another way using async/await. First create a delay function:

    function delay(){
        return new Promise((resolve) => {
             setTimeout(() => resolve(), 1000)
        });
    }
    

    And afterwards, use await on it. Note that to use it, the scope has to be async.

    async function main() {
        for(let i = 0; i < 5; i++) {
             console.log(i);
             await delay();
        }
    }