问题描述
css
.item {
display:none;
}
html
< div>
< div class =item> machin< / div>
< div class =item>选择< / div>
< div class =item> chouette< / div>
< div class =item> prout< / div>
< / div>
我正在使用jQuery,我想让每个 .item
出现在随机的小计时器之后:
javascript
$('。item')。each(function(){
itm = $(this);
setTimeout(function(){
itm.fadeIn(1000);
},Math.floor(Math.random()* 1000));
})
这里 itm
将始终包含最后一项,因为在所有作业完成后评估该函数。
我不能使用 setTimeout()
的第3个参数,因为它不适用于IE。
不建议使用 setTimeout()
出于安全原因使用 eval 方法。
那么我怎样才能通过 setTimeout()
?
编辑
我知道这个问题已经发布了。
但是我觉得它有点规格使用每个()
上下文.-
现在有人完全改变了我的问题的标题,这个标题最初类似于'setTimeout() - jQuery.each()这个对象参数'
不要使用setTimeout,使用jQuery自己的工具。 $(this).delay(数学。) random()* 1000).fadeIn();
})
工作示例:
css
.item {
display: none;
}
html
<div>
<div class="item">machin</div>
<div class="item">chose</div>
<div class="item">chouette</div>
<div class="item">prout</div>
</div>
I'm using jQuery and I'd like to make each .item
appearing after a random little timer like:
javascript
$('.item').each(function () {
itm = $(this);
setTimeout(function () {
itm.fadeIn(1000);
}, Math.floor(Math.random() * 1000));
})
Here itm
will always contain the last item because the function is evaluated after all assignments.
I can't use the 3rd parameter of setTimeout()
because it will not work on IE.
It's not advised to use setTimeout()
with the eval method for security reasons.
So how can I access to my object through setTimeout()
?
Edit
I know that this question have already been posted.
But I though that it were slightly specific with the each()
context.
Now someone have entirely changed the title of my question that was originally something like 'setTimeout() - jQuery.each() this object parameter'
Do not use setTimeout, use jQuery own tools.
$('.item').each(function () {
$(this).delay(Math.random() * 1000).fadeIn();
})
Working example: http://jsfiddle.net/qENhd/
这篇关于如何将'this'传递给setTimeout回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!