[英]JavaScript / jQuery : restart for loop on last iteration
I am trying to make a for loop that starts over when it reaches the last iteration. 我试图做一个for循环,当它到达最后一次迭代时重新开始。
Here's what I have so far: 这是我到目前为止的内容:
for (var i = 0; i < x + 3; i++) {
(function(i){
setTimeout(function(){
$('#e'+i).animate({'color': '#B2E4FF'}, 500);
var j = i - 3;
$('#e'+j).animate({'color': '#A6A5A6'}, 500);
}, 100 * i);
}(i));
if(i == x + 2) {
i = -1;
continue;
}
}
When I add continue;
当我添加时
continue;
to the script, it stops working completely. 脚本,它将完全停止工作。
What I want to achieve with this is an animated sliding gradient text. 我要实现的是动画滑动渐变文本。 DEMO 1 LOOP: http://puu.sh/aCzrv/98d0368e6b.mp4
演示1循环: http : //puu.sh/aCzrv/98d0368e6b.mp4
Full Code: http://jsfiddle.net/D6xCe/ 完整代码: http : //jsfiddle.net/D6xCe/
Regards, 问候,
Alex 亚历克斯
Dont abuse the for
loop. 不要滥用
for
循环。
Use a while
loop for that. 为此使用
while
循环。
var isCompleted = false;
while(!isCompleted){
// do logic and when completed assign isCompleted = true;
}
Edit
编辑
Due to your request, it should look something like this: 根据您的要求,它应该看起来像这样:
var isCompleted = false;
var i = 0;
while (!isCompleted) {
(function (i) {
setTimeout(function () {
$('#e' + i).animate({
'color': '#B2E4FF'
}, 500);
var j = i - 3;
$('#e' + j).animate({
'color': '#A6A5A6'
}, 500);
}, 100 * i);
}(i));
if (i == x + 2) {
i = 0;
} else if (i == x + 3) {
isCompleted = true;
}
i++;
}
There is a problem with your code
您的代码有问题
You say that if i < x+3 -> break the loop
and if i == x+2 -> reset the index and start again.
您说如果
i < x+3 -> break the loop
并且如果i == x+2 -> reset the index and start again.
Since i is incremented by 1 on each iteration, you are trying to perform an "infinite loop". 由于在每次迭代中i都会增加1,因此您尝试执行“无限循环”。 Use
while(true)
for that. 为此使用
while(true)
。
You will never get to the i == x+3
condition so you can remove it from the code I've added and get an infinite loop that does you logic based on resetting after x+2 iterations. 您将永远不会达到
i == x+3
条件,因此您可以将其从我添加的代码中删除,并获得一个无限循环,该循环会根据x + 2迭代后的重置进行逻辑运算。
var i = 0;
while (true) {
(function (i) {
setTimeout(function () {
$('#e' + i).animate({
'color': '#B2E4FF'
}, 500);
var j = i - 3;
$('#e' + j).animate({
'color': '#A6A5A6'
}, 500);
}, 100 * i);
}(i));
i = i == x+2 ? 0 : i+1;
}
You can wrap animation code in function and repeat this function again after the loop is over: 您可以将动画代码包装在函数中,并在循环结束后再次重复此函数:
function animate() {
for (var i = 0; i < x + 3; i++) {
(function (i) {
setTimeout(function () {
var j = i - 3;
$('#e' + i).animate({color: '#B2E4FF'}, 500);
$('#e' + j).animate({color: '#A6A5A6'}, 500, function() {
if (i == x + 2) {
animate();
}
});
}, 100 * i);
}(i));
}
}
animate();
if you want to perform a continues execution of certain statement just make an infinitive loop.There are many ways to form a infinitive loop the easiest way is to use while loop. 如果要执行某个语句的继续执行,只需要进行不定式循环。形成不定式循环的方法有很多,最简单的方法是使用while循环。 eg:
例如:
while(true)
{
//your statements
}
thank you 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.