使用setTimeout在无限循环中创建
问题描述:
我想弄清楚如何在达到列表的最后一项后重新启动循环。基本上它每3秒就通过active
到下一个兄弟姐妹。一旦到达最后一个项目,我如何才能将它传递给列表中的第一个项目。使用setTimeout在无限循环中创建
我想也许如果我能有使用if/else
代替for
环在我的计划
$(document).ready(function() {
\t function durationSlider() {
\t var listItems = $('.loop ul li').length;
\t \t for(count=0; count <= listItems - 1; count++) {
(function(count) {
setTimeout(function() {
\t \t \t \t \t $('.loop ul li.active').removeClass('active');
\t \t \t \t \t $('.loop ul li:eq(' + count + ')').addClass('active'); \t
\t \t \t \t \t console.log(count);
}, 3000 * count);
}(count));
\t \t } \t \t
\t }
\t durationSlider();
})
\t
/* 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 {
\t margin: 0;
\t padding: 0;
\t border: 0;
\t font-size: 100%;
\t font: inherit;
\t vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
\t display: block;
}
body {
\t line-height: 1;
}
ol, ul {
\t list-style: none;
}
blockquote, q {
\t quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
\t content: '';
\t content: none;
}
table {
\t border-collapse: collapse;
\t border-spacing: 0;
}
.loop ul li {
\t color : green;
\t -webkit-transition: color 1s linear;
}
.loop ul li.active {
\t -webkit-transition: color 1s linear; \t
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
,而不是一个for循环与setTimeout
。在间隔内,我们可以添加一个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'); \t
console.log(count);
count += 1;
if (count >= listItems) {
count = 0;
}
}, 3000);
}
durationSlider();
})
\t
/* 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 {
\t margin: 0;
\t padding: 0;
\t border: 0;
\t font-size: 100%;
\t font: inherit;
\t vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
\t display: block;
}
body {
\t line-height: 1;
}
ol, ul {
\t list-style: none;
}
blockquote, q {
\t quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
\t content: '';
\t content: none;
}
table {
\t border-collapse: collapse;
\t border-spacing: 0;
}
.loop ul li {
\t color : green;
\t -webkit-transition: color 1s linear;
}
.loop ul li.active {
\t -webkit-transition: color 1s linear; \t
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>
+0
谢谢你回答,对不起,我的无知有什么区别使用'setTimeout'和'setInterval'? – clestcruz
+1
'setTimeout'是在指定的毫秒数之后执行的一次。 'setInterval'将每次以指定的毫秒数执行(直到用'clearInterval'停止)。 –
答
使用纯JavaScript代码
function start() {
setTimeout(function() {
console.log('Hello My Infinite Loop Execution');
// Again
start();
// Every 3 sec
}, 3000);
}
// Begins
start();
如果你试图与'setTimeout'你可能想OT使用一个无限循环'setInterval'代替 –