1

I have an array such as this:

var array = [['h','e','l','l','o'],['1','2','3','4','5'],['a','b','c','d','e']]

and I am having trouble passing it to a function here is the original script I am using:

for (var x = 0; x <= 2; x++) {
    var timesrun = 0;

    function runcode() {
        timesrun += 1;
        for (var n = 0; n <= 4; n++) {
            console.log(array[x][n]);
        } //end for loop 1
        if (timesrun == 2) {
            clearInterval(interval);
        }
    } //end of function
} // end for loop 2
var interval = setInterval(function () {
    runcode(array[x]);
}, 1000);

When I console.log inside the function I get nothing but if I take the inner for loop outside the function and then console.log I get the expected values So I don't think I'm bringing the values into the function correctly.

For simplicity's sake I would like to ask this question using the simple example below:

function runcode(?){
    console.log(array[0][1]);  //which should return h.
}
runcode(?);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Keleko
  • 245
  • 4
  • 15
  • possible duplicate of [Javascript closure inside loops - simple practical example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Bergi Apr 24 '13 at 08:55

2 Answers2

1
var array = [['h','e','l','l','o'],['1','2','3','4','5'],['a','b','c','d','e']],
    x = 0,
    timesrun = 0;

function runcode() {
    timesrun += 1;
    for (var n = 0; n <= 4; n++) {
        console.log(array[x][n]);
    }
    if (timesrun == 2) {
        clearInterval(interval);
    }
}    

var interval = setInterval(function () {
    for (x = 0; x <= 2; x++) {
        runcode(array[x]);
    }
}, 1000);
  • He wants to use `setInterval`! – Bergi Apr 24 '13 at 08:56
  • 1
    Thank you! you saved many hours of anguish lol. I think I can definitely continue from here :) – Keleko Apr 24 '13 at 09:15
  • although it does seem I must use array[x-1][n] inside the function to get all of them or else it crashes cause it skips the first one. would you maybe know why this is? – Keleko Apr 24 '13 at 09:21
  • What is `setInterval` doing there, its essentially the same as doing a `setTimeout` as on the fist invocation `timesrun === 3` hence the interval is getting cleared. Also the array passed to `runcode` is never used in it. And i think thats waht the OP's question was about – Moritz Roessler Apr 25 '13 at 14:47
0

For simplicity's sake I would like to ask this question using the simple example below:

function runcode(?){
    console.log(array[0][1]);  //which should return h.
}

runcode(?);

To access the a passed variable, you have to name the parameters in the functions header

so if you would replace ? with array you are already good to go

function runcode(array){
              // ^^^^^ This is the name under which you can acces the passed variable
    console.log(array[0][1]);  //which should return h.
              //^^^^^ This is where you use it

}
runcode(array);
      //^^^^^ this is where you pass it //could be someOther2DimensionalArray

And applied to the nonworking code

var array = [
    ['h', 'e', 'l', 'l', 'o'],
    ['1', '2', '3', '4', '5'],
    ['a', 'b', 'c', 'd', 'e']
],
    x = 0,
    timesrun = ~0;

function runcode(array) {
    x = timesrun += 1;
    for (var n = 0; n <= 4; n++) {
        console.log(array[x][n]);
    }
    if (timesrun == 2) {
        clearInterval(interval);
    }
}

var interval = setInterval(runcode, 1000, array);

this should do the trick.

Note the third parameter of setInterval is used to pass array //runcode(array)

Moritz Roessler
  • 8,542
  • 26
  • 51