使用setTimeout在无限循环中创建 [英] Creating in an infinite loop using setTimeout

查看:43
本文介绍了使用setTimeout在无限循环中创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚到达列表的最后一项后如何重新启动循环.基本上,它每3秒钟将类active传递给下一个同级.一旦到达最后一项,如何将其传递到列表中的第一项.

I'm trying to figure out how I can restart my loop after reaches the last item of the list. Basically it passes the class active to the next sibling every 3s. How can I make it pass to the first item on the list once it reaches the last item.

我在想也许我可以在程序中使用if/else而不是for循环

I was thinking maybe if I could have use if/else instead of for loop in my program

$(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>

推荐答案

我将使用setInterval代替setTimeout的for循环.在间隔内,我们可以添加一个if条件,以在计数到达列表末尾时重置计数.

I would use setInterval instead of a for loop with setTimeout. Inside the interval we can add an if condition to reset the count when it reaches the end of your list.

$(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>

这篇关于使用setTimeout在无限循环中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆