Javascript 运行函数x的次数
我试图让一个div每30秒出现一次,但只出现5次。我的代码工作得很好,只是在div中循环并每隔30秒显示一个div,但我需要它在完成5次之后停止Javascript 运行函数x的次数,javascript,jquery,Javascript,Jquery,我试图让一个div每30秒出现一次,但只出现5次。我的代码工作得很好,只是在div中循环并每隔30秒显示一个div,但我需要它在完成5次之后停止 $(window).bind("load", function flashing() { var counter = 0, divs = $('.flash-image'); function showDiv () { divs.hide() // hide all divs .filter(func
$(window).bind("load", function flashing() {
var counter = 0,
divs = $('.flash-image');
function showDiv () {
divs.hide() // hide all divs
.filter(function (index) { return index == counter % 30; })
.show('fast');
counter++;
};
showDiv(); // show first div
setInterval(function () {
showDiv(); // show next div
}, 1 * 1000); // do this every 1 seconds
});
$(窗口).bind(“加载”,函数闪烁(){
变量计数器=0,
divs=$(“.flash image”);
函数showDiv(){
hide()//隐藏所有div
.filter(函数(索引){返回索引==计数器%30;})
.show(“fast”);
计数器++;
};
showDiv();//显示第一个div
var inter=setInterval(函数(){
如果(计数器<5){
showDiv();//显示下一个div
}否则{
间隔时间;
}
},1*1000);//每1秒执行一次
});
你也可以使用你的计数器
变量,我添加了数量,所以我不需要复制孔代码。由于计数器循环,间隔结束后,div一直在页面上闪烁,所以我不得不使用。隐藏它有点乱,但正在执行rickcode
showDiv();//show first div var inter=setInterval(函数(){if(counter<120){showDiv();//show next div}否则{clearInterval(inter);divs.hide()},1*1000);//每1秒执行一次})<代码>代码
var amount = 0;
var timerId = setInterval(function () {
showDiv(); // show next div
amount++;
if(amount === 5) {
clearInterval(timerId);
}
}, 1 * 1000); // do this every 1 seconds
$(window).bind("load", function flashing() {
var counter = 0,
divs = $('.flash-image');
function showDiv () {
divs.hide() // hide all divs
.filter(function (index) { return index == counter % 30; })
.show('fast');
counter++;
};
showDiv(); // show first div
var inter = setInterval(function () {
if(counter < 5){
showDiv(); // show next div
}else{
clearInterval(inter);
}
}, 1 * 1000); // do this every 1 seconds
});