1

我试图让 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(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 
});
4

2 回答 2

3
var amount = 0;
var timerId = setInterval(function () {
    showDiv(); // show next div
    amount++;
    if(amount === 5) {
        clearInterval(timerId);
    }
}, 1 * 1000); // do this every 1 seconds 
于 2013-06-27T16:23:04.037 回答
0
 $(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 
});
于 2013-06-27T16:24:27.657 回答