setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。语法:setTimeout(function,delaytime);
只执行一次,如果想多次执行只需要调用本身
<div>
<label for="Text">Text1</label>
<input type="text" id="txt1" readonly>
</div>
<div>
<label for="Text">Text2</label>
<input type="text" id="txt2" readonly>
</div>
<div>
<label for="Text">Text3</label>
<input type="text" id="txt3" readonly>
</div>
var txtData = {
text: [[1111, 2222, 3333],
[444, 555, 666],
[233, 244, 255],
[266, 277, 288],
[299, 300, 301],
[666, 777, 888]
]
};
var $text1 = document.getElementById('txt1'),
$text2 = document.getElementById('txt2'),
$text3 = document.getElementById('txt3'),
length = txtData.text.length;
var arrays = new Array();
function equalValue(array) {
$text1.value = array[0];
$text2.value = array[1];
$text3.value = array[2];
arrays.push($text1);
arrays.push($text2);
arrays.push($text3);
/*alert(array.join(','));*/
}
/*window.onload = function() {
for (let i = 0; i < length; i++) {
setTimeout(equalValue(txtData.text[i]), 10000);
}
}*/这个并没有得到想要的结果,循环的时间忽略
我们可以通过调用自身来实现循环显示。
var
i
=
0
;
window
.
onload
=
function
setValue
() {
i
=
i
>
size
-
1
?
i
=
1
:
i
+
1
;
equalValue
(
votingData
.
voting
[
i
-
1
]);
setTimeout
(
setValue
,
100
);
}