troychroi
troychroi

Reputation: 49

setInterval stops after once

// .items direct child
var itemsContainer = $('.blog-ticker').children();
// each item in .items
var items = $('.blog-ticker').children().children();        
setInterval(function(){
    var i = 0;
    itemsContainer.css({
        'top': -(items[i].clientHeight)+'px'
    });
    if(i <= items.length) {
        i++;
    }
}, 2000);

it stops after the first round... confusion sets in. What Should I do?

Upvotes: 0

Views: 881

Answers (1)

Rhumborl
Rhumborl

Reputation: 16609

No it probably doesn't stop after the first round.

The problem is that you are declaring i to 0 each time the function runs, so you are just setting the first item to the same position and it appears to be doing nothing.

You just need to declare i outside of the function and it should work fine:

// .items direct child
var itemsContainer = $('.blog-ticker').children();
// each item in .items
var items = $('.blog-ticker').children().children();        
// counter
var i = 0;
setInterval(function(){
    itemsContainer.css({
        'top': -(items[i].clientHeight)+'px'
    });
    if(i <= items.length) {
        i++;
    }
}, 2000);

Upvotes: 2

Related Questions