1

8x10 のセルを含むテーブルを取得しました..各販売は、独自の ID (11、12、...、21、22、23、...) を持つ入力要素を取得しました。 0.5秒と言います)テスト用にいくつかの値を入力しました

        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];
            }
          }

コンパイラは、「TypeError: 未定義のプロパティ '11' を読み取ることができません」と言います

settimeout() 関数を使用しない場合、ループ全体が正常に機能しますが、この関数ではこの間違いが発生します..

4

4 に答える 4

2

これは、希望どおりに機能するはずです。

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);
    }
}

物事をもう少し明確にするために、次のようにリファクタリングすることを検討してください。

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];
}
于 2013-02-14T13:19:44.540 に答える
2

次のようなことを試すことができます:

    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);

http://jsfiddle.net/Ex98V/の実行例を次に示します。

于 2013-02-14T13:17:04.427 に答える