Wait for a run function to perform the next

Asked

Viewed 34 times

-4

I’m making a script in JS but I’m having a little difficulty.

I wanted to make my Loop() function be called again only when my Fatown() function has already been fully executed, today when I run Script the Loop() function is called before the Fatown() function is fully executed

Follow the code excerpt

(function loop() {
  var rand = Math.round(Math.random() * (minutoMax - minutoMin)) + minutoMin;
  console.log(new Date().toLocaleString());
  setTimeout(function() {
    FaTown();
    loop();
  }, rand);
}());

function FaTown() {
  setTimeout(function() {
    uw.FarmTown.openFarm();
    setTimeout(function() {
      try {
        $('.checkbox.selcionar_tudo').click();
      } catch (e) {
        //
      }
      setTimeout(function() {
        document.getElementById("pegar_button").click()
        setTimeout(function() {
          $('.btn_confirmar_sim').click();
          setTimeout(function() {
            var janelas = GPWindowMgr.getOpenedClosableWindows();
            for (var i = 0; i < janelas.length; i++) {
              if (janelas[i].type === 589) {
                janelas[i].close();
                break;
              }
            }
          }, timeOpen)
        }, time1)
      }, time2)
    }, time3)
  }, time4)
}

  • 1

    I don’t understand anything. Explain better and in more detail what you have tried.

  • I’m doing the question Edit

1 answer

0


It seems to me that you are simulating an interval between one click and the other with these setTimeout.

You can get the same result using await within an asynchronous function, and as a result you could also use the await to wait for function resolution before rotating it again:

async function sleep(time) {
    return new Promise(resolve => setTimeout(resolve, time));
}

async function FaTown() {
    await sleep(time4);
    uw.FarmTown.openFarm();

    await sleep(time3);
    const checkbox = $('.checkbox.selcionar_tudo');
    if (checkbox) checkbox.click();

    await sleep(time2);
    document.getElementById('pegar_button').click();

    await sleep(time1);
    $('.btn_confirmar_sim').click();

    await sleep(timeOpen);
    const janelas = GPWindowMgr.getOpenedClosableWindows();
    for (let i = 0; i < janelas.length; i++) {
        if (janelas[i].type === 589) {
            janelas[i].close();
            break;
        }
    }
}

(async function loop() {
    const rand = Math.round(Math.random() * (minutoMax - minutoMin)) + minutoMin;
    console.log(new Date().toLocaleString());
    await sleep(rand);

    await FaTown();
    loop();
})();

But this is a suggestion, if you intend to continue with the setTimeout can use a callback function for the FaTown invoke after finished executing, in which case, that function would be itself loop:

function FaTown(callback) {
    setTimeout(function () {
        uw.FarmTown.openFarm();
        setTimeout(function () {
            try {
                $('.checkbox.selcionar_tudo').click();
            } catch (e) {
                //
            }
            setTimeout(function () {
                document.getElementById("pegar_button").click()
                setTimeout(function () {
                    $('.btn_confirmar_sim').click();
                    setTimeout(function () {
                        var janelas = GPWindowMgr.getOpenedClosableWindows();
                        for (var i = 0; i < janelas.length; i++) {
                            if (janelas[i].type === 589) {
                                janelas[i].close();
                                break;
                            }
                        }
                        callback();
                    }, timeOpen)
                }, time1)
            }, time2)
        }, time3)
    }, time4)
}

(function loop() {
    var rand = Math.round(Math.random() * (minutoMax - minutoMin)) + minutoMin;
    console.log(new Date().toLocaleString());
    setTimeout(function () {
        FaTown(loop);
    }, rand);
}());

More about async/await and callback function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.