javascript using settimeout() with a loop

后端 未结 4 1624
醉酒成梦
醉酒成梦 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:58

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

提交回复
热议问题