使用setTimeout创建无限循环

3

我正在尝试弄清楚如何在到达列表的最后一个项目后重新启动循环。基本上,它会每3秒将类 active 传递给下一个兄弟节点。一旦到达最后一个项目,如何使其传递到列表中的第一个项目。

我考虑过在程序中使用 if/else 而不是 for 循环。

$(document).ready(function() {



 function durationSlider() {
     var listItems = $('.loop ul li').length;

  for(count=0; count <= listItems - 1; count++ ) {
         
            (function(count) {
                setTimeout(function() {
     $('.loop ul li.active').removeClass('active');
     $('.loop ul li:eq(' + count +   ')').addClass('active'); 
     console.log(count);
                }, 3000 * count);
            }(count));
  }  
 }

 durationSlider();
    

})
 
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
 margin: 0;
 padding: 0;
 border: 0;
 font-size: 100%;
 font: inherit;
 vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
 display: block;
}
body {
 line-height: 1;
}
ol, ul {
 list-style: none;
}
blockquote, q {
 quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
 content: '';
 content: none;
}
table {
 border-collapse: collapse;
 border-spacing: 0;
}


.loop ul li {
 color : green;
 -webkit-transition: color 1s linear;

}

.loop ul li.active {
 -webkit-transition: color 1s linear; 
    color : red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="loop">
    <ul>
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
</div>


1
如果您想使用setTimeout创建一个无限循环,您可能需要使用setInterval - A. L
2个回答

8

使用纯JavaScript代码

function start() {

    setTimeout(function() {
        console.log('Hello My Infinite Loop Execution');

      // Again
      start();

      // Every 3 sec
    }, 3000);
}

// Begins
start();

7
我建议你使用setInterval而不是带有setTimeout的for循环。在定时器中,我们可以添加一个if条件语句,以便在到达列表末尾时重置计数。

$(document).ready(function() {

function durationSlider() {
    var listItems = $('.loop ul li').length;
    var count = 0;
     
    setInterval(function() {
        $('.loop ul li.active').removeClass('active');
        $('.loop ul li:eq(' + count +   ')').addClass('active'); 
        console.log(count);

        count += 1;
        if (count >= listItems) {
            count = 0;
        }
    }, 3000);

}

durationSlider();

})
 
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
 margin: 0;
 padding: 0;
 border: 0;
 font-size: 100%;
 font: inherit;
 vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
 display: block;
}
body {
 line-height: 1;
}
ol, ul {
 list-style: none;
}
blockquote, q {
 quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
 content: '';
 content: none;
}
table {
 border-collapse: collapse;
 border-spacing: 0;
}


.loop ul li {
 color : green;
 -webkit-transition: color 1s linear;

}

.loop ul li.active {
 -webkit-transition: color 1s linear; 
    color : red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="loop">
    <ul>
        <li class="active">1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
</div>


非常感谢您的回答,对不起我很愚昧,请问使用setTimeoutsetInterval有什么区别? - clestcruz
1
setTimeout 是在指定的毫秒数后执行一次的定时器。setInterval 则会每隔指定的毫秒数执行一次(直到使用 clearInterval 停止)。 - Tom Van Rompaey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接