[英]How to use 2 loops alternately in javascript?
I have two loops. 我有两个循环。 How can I run them alternately?
如何交替运行它们? For example output of this code:
例如此代码的输出:
for (var i = 15; i >= 10; i--) {
console.log(i)
}
for (var t = 15; t <= 20; t++) {
console.log(t)
}
will be: 将会:
15 // first loop started
14
13
12
11
10 // first loop ended
15 // second loop started
16
17
18
19
20 // second loop ended
What should i do to get this output: 我应该怎么做才能得到这个输出:
15 //first loop started
15 //second loop started
14 //first
16 //second
13 //first
17 //second
12 //first
18 //second
11 //first
19 //second
10 //first loop ended
20 //second loop ended
You could put both initialization , condition and final-expression parts in one for loop. 您可以将initialise , condition和final-expression部分都放在一个for循环中。
for (var i = 15, t = 15; i >= 10 && t <= 20; i--, t++) { console.log(i) console.log(t) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.