jQuery的 - 添加类元素中与延迟阵列 [英] jQuery - Adding classes to elements inside array with delay
问题描述
我有一个包含从我的网页一些元素的数组。
I have an array that contains some elements from my page.
现在我需要通过数组识字,并增加了一类粗体
来的每个元素的功能。问题是,一旦类被添加,一些时间已经过去。然后粗体
已被删除,并需要被施加到下一个元素,从而产生波的运动。
Now I need a function that literates through the array and adds a class bold
to each element. The problem is that once the class is added, some time has to pass. Then bold
has to be removed and needs to be applied to the next element, resulting in a "wave" motion.
我一直试图做这样的:
$.each(tdArr, function(i, v) {
v.addClass("bold");
setTimeout(function(){
v.removeClass("bold");
}, 900)
})
与code的问题是,粗体
添加到的所有的一次元素会从900毫秒后取出,再的所有的在同一时间的元素。
The problem with that code is that bold
is added to all elements at once and gets removed 900 ms later, again from all elements at the same time.
什么我必须做单独的行动之间添加延迟?
What do I have to do to add a delay between the separate actions?
推荐答案
我认为你需要稍微不同的方法处理这一点,因为 setTimout
返回直线距离它不会阻止下元素被设置为粗体。
I think you need to approach this slightly differently, as setTimout
returns straight away it wont stop the next element being set as bold.
您可以用这样的方法做到这一点:
You could do it with a method like this:
function bold(i){
if(i>0){
$tdArr.eq(i-1).removeClass('bold');
}
if(i == $tdArr.length){
return;
}
$tdArr.eq(i).addClass('bold')
setTimeout(function() { bold(i+1) },900);
}
然后调用它你只需要调用粗体(0)
:
活生生的例子: http://jsfiddle.net/rJGjZ/
这篇关于jQuery的 - 添加类元素中与延迟阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!