javascript using settimeout() with a loop

后端 未结 4 1622
醉酒成梦
醉酒成梦 2021-01-24 20:21

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)

4条回答
  •  爱一瞬间的悲伤
    2021-01-24 20:55

    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.

提交回复
热议问题