Michael Sanker
Michael Sanker

Reputation: 13

javascript using settimeout() with a loop

ive got a table with 8x10 cells.. each sell got an input element with its own id (11, 12, ... , 21,22,23,...) now i want to fill these inputs after and after (lets say 0.5 sec) i just entered some values for testing

        Betrag = new Array();
        Betrag[0] = new Array();
        Betrag[1] = new Array("asd","asd","asd","asd","asd","asd","asd","asd","asd","asd","513.000,00");
        Betrag[2] = new Array("asd","adsd","asd","asd","asd","asd","asd","asd","asd","asd","asd");
        Betrag[3] = new Array("asd","asd","asd","asd","asd","asd","asd","asd","asd","asd","asd");
        Betrag[4] = new Array("asd","uisgui","asd","asd","asd","asd","asd","asd","asd","asd","asd");
        Betrag[5] = new Array("asd","asd","asd","asd","asd","asd","asd","asd","asd","asd","asd");
        Betrag[6] = new Array("asd","asd","asd","asd","asd","asd","asd","asd","asd","asd","asd");
        Betrag[7] = new Array("asd","asd","asd","asd","asd","asd","asd","asd","asd","asd","asd");
        Betrag[8] = new Array("asd","asd","asd","asd","asd","asd","asd","asd","asd","asd","asd");

          for(i=1; i<=8; i++){
            for(k=1; k<=10; k++){
              setTimeout(function schreiben(){document.getElementById(''+i+k+'').value= Betrag[i][k];}, 1000);
              //document.getElementById(''+i+k+'').value= Betrag[i][k];
            }
          }

the compiler says "TypeError: Cannot read property '11' of undefined"

if i would not use the settimeout() function the whole loop is working fine, but with this function ive got this mistake..

Upvotes: 1

Views: 2270

Answers (4)

Anubhav Ranjan
Anubhav Ranjan

Reputation: 1598

You can try something like this:

    var i = 1;
    var k = 1;
    var obj = setInterval( function () {
        document.getElementById(i + '' + k).value= Betrag[i][k];
        if(k <= 10)
           k++;
        else
        {
            k = 1;
            if(i<=8)
                 i++;
            else
                 clearInterval(obj);
        }
    }, 1000);

Here's a running example at http://jsfiddle.net/Ex98V/

Upvotes: 2

Frances McMullin
Frances McMullin

Reputation: 5696

This should work the way you wanted.

for(i=1; i<=8; i++){
    for(k=1; k<=10; k++){
        (function(i, k){
            setTimeout(function schreiben(){document.getElementById(''+i+k+'').value= Betrag[i][k];}, 1000*k + 10000*i);
            //document.getElementById(''+i+k+'').value= Betrag[i][k];
        })(i, k);
    }
}

To make things a bit clearer, consider refactoring like this :

for(i=1; i<=8; i++){
    for(k=1; k<=10; k++){
        setSchreibTimeout(i, k);
    }
}

function setSchreibTimeout(i, k){
    setTimeout(function schreiben(){document.getElementById(''+i+k+'').value= Betrag[i][k];}, 1000*k + 10000*i);
    //document.getElementById(''+i+k+'').value= Betrag[i][k];
}

Upvotes: 2

Bart Friederichs
Bart Friederichs

Reputation: 33511

k and i are read after the for loop ended (1 second to be precise). Their values are then 9 and 11, leading to an array overflow problem.

One option to fix it, is to create a function that does the work, and create a fixed string from the k and i variables to call it:

function schreiben(__i, __k) {
   document.getElementById(''+__i+__k+'').value= Betrag[__i][__k];
}

Then call the setTimeout like this:

setTimeout("schreiben("+i+","+k+")", 1000);

Not the most elegant way, but it works.

Upvotes: 0

diegoGarc
diegoGarc

Reputation: 41

Bart Friederichs is right. Not sure why you want to do this that way, but you can declare a couple of vars inside the schreiben function and increment them inside the same screiben function.

Upvotes: 0

Related Questions