1

I have a script that changes some values and clicks some buttons in a browser. I'm looking for a way to pause the execution of the code for x seconds without making the browser lag out. I'm using Firefox and I paste this script into its console. I'm aware of the setTimeout() function. However, this function doesn't stop executing the code, but rather waits x seconds till executing it. I'm looking for a complete solution that's similar to Python's time.sleep() function and pauses the execution of the code completely.

var init = 0.01
var start = init
var $odds = $("#oddsOverUnder")
var $button = $("#roll")
var $bet = $("#bet")

function roll(){
    $bet.val(start)
    $button.click()
    //i want to pause the execution of the code here//
    $button.click()
    //and here//
    refreshIntervalId=setInterval(roll2, 2000)}

function roll2(){
    var tr = document.querySelector("#myBetsTable tr:nth-child(2)")
    var cls = tr.getAttribute('class')
    if (cls === 'success'){
        start = init
        $bet.val(start)}
    else{
        start = start * 2
        $bet.val(start)
        $odds.click()}

clearInterval(refreshIntervalId)
roll()}

roll()
Recuvan
  • 161
  • 5

2 Answers2

1

How about setting timeouts inside a timeout?

function roll() {
    $bet.val(start)
    $button.click()
    setTimeout(function() {
            $button.click();
            setTimeout(
                function() {
                    $button.click();
                    refreshIntervalId = setInterval(roll2, 2000)
                },
                5000
            );
        },
        5000
    );
}

also, please see: https://stackoverflow.com/a/951057/2119863

Community
  • 1
  • 1
Unamata Sanatarai
  • 6,475
  • 3
  • 29
  • 51
1

There's no equivalent to Python's time.sleep() in JavaScript, which generally executes asynchronously by design. If you'd like to delay execution of code, you can use setTimeout, as you mentioned.

Without understanding exactly what your code is supposed to do, I can't suggest the ideal architecture for this problem. There's definitely a better way to do it than what I'm about to suggest. Still, to answer your question, you can simulate what you're asking this way:

function roll() {

    $bet.val(start);
    $button.click();

    setTimeout(function () {
        $button.click();
        setTimeout(function () {
           refreshIntervalId = setInterval(roll2, 2000);
        }, 1000);
    }, 1000);

}
Mark
  • 559
  • 3
  • 11