[英]How to add delay in javascript for loop using setTimeout?
I want to hold the iteration of the for-loop
until the functionality in the previous iteration gets completed. 我想保留
for-loop
的迭代,直到上一个迭代中的功能完成为止。 I have seen many questions about setTimeout
but none of them has satisfied my requirement. 我已经看到了许多有关
setTimeout
问题,但没有一个满足我的要求。 Can anyone help me? 谁能帮我?
function abcd() {
var exArray = [0, 0, 1, 1, 1, 0, 0, 0];
var index = 0;
for (var i = 0; i < exArray.length; i++) {
if (exArray[i] == 1) {
addDelay(++index);
} else {
console.log('ended at ' + i);
}
}
}
seTimeout functionality is something like this seTimeout功能是这样的
function addDelay(index) {
setTimeout(function() {
console.log("in Timeout loop:" + index);
}, 2000 * index);
}
ended at 0 结束于0
ended at 1 结束于1
in Timeout loop:{{index}} // wait for delay 在超时循环中:{{index}} //等待延迟
in Timeout loop:{{index}} // wait for delay 在超时循环中:{{index}} //等待延迟
in Timeout loop:{{index}} // wait for delay 在超时循环中:{{index}} //等待延迟
ended at 5 结束于5
ended at 6 结束于6
ended at 7 结束于7
ended at 0 结束于0
ended at 1 结束于1
ended at 5 结束于5
ended at 6 结束于6
ended at 7 结束于7
in Timeout loop:{{index}} // wait for delay 在超时循环中:{{index}} //等待延迟
in Timeout loop:{{index}} // wait for delay 在超时循环中:{{index}} //等待延迟
in Timeout loop:{{index}} // wait for delay 在超时循环中:{{index}} //等待延迟
I want the ended at 5
to wait until timeout functionality of 3rd, 4th, 5th array indices. 我希望
ended at 5
直到第3,第4,第5个数组索引的超时功能。 I need someone to help me on this. 我需要有人来帮助我。 Thanks in advance.
提前致谢。
Hope I understand your question, please see this Fiddle. 希望我理解您的问题,请参阅此提琴。 https://fiddle.jshell.net/qw6qp7pm/1/ (Using Underscore JS
delay()
http://underscorejs.org/#delay ) https://fiddle.jshell.net/qw6qp7pm/1/ (使用Underscore JS
delay()
http://underscorejs.org/#delay )
function abcd(index) {
var exArray = [0, 0, 1, 1, 1, 0, 0, 0]
var index = index ? index : 0
if (index >= exArray.length) return false
for (var i = index; i < exArray.length; i++) {
if (exArray[i] == 1) {
//addDelay(++index);
_.delay(log, 2000, i)
break
} else {
console.log('ended at ' + i)
}
}
}
function log(index) {
console.log("in Timeout loop:" + index)
// RECURSE
abcd(index + 1)
}
abcd()
loop will execute continuously for its each index. 循环将为其每个索引连续执行。 as you need all your logs one-after-one, means you have to put delay for each index, whether its satisfy your condition or not.
因为您需要一个接一个的所有日志,这意味着您必须为每个索引设置延迟,无论其是否满足您的条件。
function abcd() {
var exArray=[0,0,1,1,1,0,0,0];
var index=0;
var message='';
for(var i=0;i<exArray.length; i++) {
if(exArray[i]==1) {
++index;
message='in Timeout loop:'+i;
addDelay(index, message);
}else{
message='ended at '+i;
addDelay(index, message);
}
}
}
function addDelay(index, message){
setTimeout(function () {
console.log(message);
}, 2000*index);
}
setTimeOut
doesn't work that way. setTimeOut
不能那样工作。 It just registers an action to be performed at a later point of time and just continues. 它只是注册要在以后的时间执行的动作,然后继续。 Once this script is over, it will take up your queued up tasks and perform.
该脚本结束后,它将处理您排队的任务并执行。
Check how setTimeout
really works under the hood. 检查
setTimeout
到底是如何工作的。 Also, read more about JavaScript's Event Loop. 另外,请阅读有关JavaScript事件循环的更多信息。
var input = [0, 0, 1, 1, 1, 0, 0, 0];
function immediateOrRegister(index) {
if (index >= input.length) return;
if (input[index] == 0) {
console.log("No delay");
immediateOrRegister(index + 1);
} else if (input[index] == 1) {
console.log("Timing out for atleast " + 2 * index + " seconds");
setTimeout(function() {
immediateOrRegister(index + 1)
}, 2000 * index);
}
}
immediateOrRegister(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.