У меня есть один массив из 10 элементов и на загрузке страницы есть 5 div, и каждый из них получит значение из этого массива, а затем используя setInterval()
, значения div будут обновляться каждые 1 секунду из остальных элементов массива.Loop throught все элементы div и update
Проблема заключается в том, что я хочу использовать только один цикл по каждому элементу и значения начинают обновлять от # 8 не из # 6 и два последних дивы не обновляются.
Fiddle: http://jsfiddle.net/90h7045b/
var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
first = data.slice(0, 5),
second = data.slice(5),
count = 0;
//update first 5 on page load
$.each($('.wrap'), function(i) {
$(this).find('.title').html(first[i]);
});
$('#container .wrap:first').addClass('current');
//it does not work with `.wrap`
$.each($('#container'), function() {
(function($set) {
var interv = setInterval(function() {
count++;
var $cur = $set.find('.current');
$cur.removeClass('current');
$cur.find('.title').html(second[count]);
var $next = $cur.next().length ? $cur.next() : $set.children().eq(0);
$next.addClass('current');
if(count == 4)
clearInterval(interv);
}, 1000);
})($(this));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div class="wrap"><div class="title"></div></div>
<div class="wrap"><div class="title"></div></div>
<div class="wrap"><div class="title"></div></div>
<div class="wrap"><div class="title"></div></div>
<div class="wrap"><div class="title"></div></div>
</div>
хорошая работа там! –
Woww, Большое спасибо! –