Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop throught all divs and update elements

I have one array of 10 elements and on page load there are 5 div and each of them will get an value from this array then using setInterval() the values of the divs will be updated every 1 second from the remaining array elements.

The problem is that I want to use only one foreach loop and the values start to update from #8 not from #6 and the two last divs are not updated.

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>
like image 274
M1X Avatar asked Dec 05 '25 10:12

M1X


2 Answers

You could simplify your code a bit. Do you really need to splice the array twice? Wouldn't it be better to use a variable for your split index and use that? Also, instead of setInterval use setTimeout.

Here is an example:

Fiddle: http://jsfiddle.net/abhitalks/90h7045b/42/

Snippet:

var data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    $wrap = $(".wrap"), 
    split = 5;

$wrap.each(function (idx) {
    var self = $(this), 
        elem = idx + split,
        timer = ((idx+1) * 1000);
    
    $(this).find('.title').html(data[idx]);
    setTimeout(function () {
        self.find('.title').html(data[elem]);
    }, timer);
});
body { text-align:center; }
.wrap {
    display: inline-block;
    background: #f3f3f3; color: #f8008c;
    padding: 5px; margin:5px; width: 64px; height:64px;
    line-height: 64px; border-radius:37px;
}
<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>
like image 152
Abhitalks Avatar answered Dec 06 '25 23:12

Abhitalks


I believe what you are wanting is for the divs to display 6 thru 10 after the code completes its execution. A simple solution I found to achieve this was to start your count variable at -2 since your code is starting its updates to late in the loop.

change your initial count variable assignment to the following code:

count = 0;

to:

count = -2;

JsFiddle example: http://jsfiddle.net/larryjoelane/90h7045b/18/

like image 36
Larry Lane Avatar answered Dec 06 '25 22:12

Larry Lane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!