[英]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 我有一个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];
}
}
the compiler says "TypeError: Cannot read property '11' of undefined" 编译器说“TypeError:无法读取未定义的属性'11'”
if i would not use the settimeout() function the whole loop is working fine, but with this function ive got this mistake.. 如果我不使用settimeout()函数整个循环工作正常,但有这个功能我有这个错误..
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/ 这是http://jsfiddle.net/Ex98V/上的一个运行示例
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];
}
k
and i
are read after the for
loop ended (1 second to be precise). 在for
循环结束后读取k
和i
(准确地说是1秒)。 Their values are then 9 and 11, leading to an array overflow problem. 它们的值为9和11,导致阵列溢出问题。
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: 解决它的一个选择是创建一个完成工作的函数,并从k
和i
变量创建一个固定的字符串来调用它:
function schreiben(__i, __k) {
document.getElementById(''+__i+__k+'').value= Betrag[__i][__k];
}
Then call the setTimeout
like this: 然后像这样调用setTimeout
:
setTimeout("schreiben("+i+","+k+")", 1000);
Not the most elegant way, but it works. 不是最优雅的方式,但它的工作原理。
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. 不确定为什么要这样做,但是你可以在schreiben函数中声明几个变量并在同一个screiben函数中增加它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.