Javascript function does not respect loop conditions

Asked

Viewed 363 times

3

Good afternoon, I’m new here and I’m using the Javascript Scratchpad of Mozila Firefox to execute codes Javascript. It will really meet my needs, however the stop condition of the while loop is not working (for also didn’t work). In addition, the variable i is not having its value increasing, preventing any value beyond the first of the array be used. I thank you for any reply, thank you.

Follows the code:

window.setInterval(function () {
   var inputs = [ 10595, 10243, 11514, 11053, 10449, 10208, 11160, 10970, 10706, 11075, 10400, 10112, 10086, 10503, 11910, 12110, 11537, 9694, 12112, 10793, 11728, 9532, 10389, 12983, 9533, 12424, 10697, 11997, 12121, 11606, 10526, 9729, 10143, 11737, 10025, 10700, 11564, 12623, 9324, 11761, 10008, 11780, 10105, 12230, 12489, 12649, 9083, 11192, 10010, 10984, 12075, 12075, 11026, 12194, 12335, 10035];
    var i = 0;
    do {
        Accountmanager.farm.sendUnits(this, inputs[i], 4202);
        i++;
    }
    while (i < 5)
}, 250);

3 answers

4

The loop is running correctly but you are externally using the function setInterval that runs the function again every 250 milliseconds, if what you want is to send all the items of your array, you can pass the size of it in the while condition.

// código do seu array omitido

var i = 0;
do {
    Accountmanager.farm.sendUnits(this, inputs[i], 4202);
    i++;
} while (i < inputs.length)

Example with the value of each item: https://jsfiddle.net/hsxanz23/

  • I understand, the problem was the interval, but I still want before every while cycle, there to be a delay, how to proceed?

  • Every few seconds you want him to send the data to this Accountmanager of yours?

  • I need that every x seconds it sends the accoun command... with the value of i incremented. I even tested his code and he only ran 1x the loop :(

  • What you were doing is that every quarter mile he sent EVERYONE, not one. @Madsonpaulo

  • The code I was using, posted on top, sent every 250ms the command with the value of the array at the position i=0.

  • No, he tried to send all the values of i every 250ms.

  • 1

    @Madsonpaulo the interval is not a Thread.sleep No, you call everyone asynchronously

Show 2 more comments

3


If I understand correctly you want to pass the values of this array one by one with a range of 250 msamong them.

You can do it like this:

function processar(dados, delay) {

    var arr = dados.slice();
    function enviar() {
        var proximo = arr.shift();
        Accountmanager.farm.sendUnits(this, proximo, 4202);
        if (arr.length) setTimeout(enviar, delay);
    }
    setTimeout(enviar, delay);
}

The idea is to create a function that processes this sending.
Every line var arr = dados.slice(); creates a copy of the array so that the original is not lost.

This solution uses setTimeout instead of setInterval that never ends until it’s called a clearInterval.

To start sending you can do processar(inputs, 250); where you choose what you send and choose speed as well.

Example: http://jsfiddle.net/xp1pk0y0/

  • 1

    Even the use of the Slice() function helped me a few days later, because I had to keep the original array to 'reload' the array used in the function (after all values are used, it is necessary to restart the array with the same elements).

0

Staff received many comments in a very short time, did not expect all this support, thank you very much, my problem was solved. The final code I’m using is:

window.setInterval(function () {
    Accountmanager.farm.sendUnits(this, inputs.shift(), 4202);
}, 250);

It was probably this shift() that makes the whole array run, I didn’t know :)

You can close the topic, thank you.

  • 1

    The shift takes an array start element. After a certain number of rounds, the array will be empty (by the way, I think you would need to check this and cancel the interval after that).

  • I put together an answer. The .shift() will fetch the first element of the array but at the same time modifying it, removing this element.

Browser other questions tagged

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