Voj*_*ina 5 javascript each jquery traffic light
我有红绿灯 - 3种颜色:
<div class="green" id="ready"></div>
<div class="orange" id="steady"></div>
<div class="red" id="go"></div>
Run Code Online (Sandbox Code Playgroud)
和数组:
var status = ["ready", "steady", "go"];
Run Code Online (Sandbox Code Playgroud)
我想在无限循环中添加和删除类(模仿闪烁)有一些延迟,但是这段代码一次性完成.我怎么解决呢?
jQuery.each(status, function() {
setTimeout(function() {
$("#" + this).addClass('active');
}, 3000);
});
Run Code Online (Sandbox Code Playgroud)
Pop*_*les 15
您将所有设置为立即运行.每次乘以索引.
$('#ready, #steady, #go').each(function(i) {
var el=$(this);
setTimeout(function() {
el.addClass('active');
}, i * 3000);
});
Run Code Online (Sandbox Code Playgroud)
注意,i在第一个instace是0,所以如果你想#ready等待3秒使用(i+1) * 3000
此外,$('#'+this)语法不正确$(this),但是在setTimeout中不起作用.
使用setInterval而不是setTimeout运行infinate(除非清除)循环.
| 归档时间: |
|
| 查看次数: |
17115 次 |
| 最近记录: |