jsg jsg - 1 year ago 50
HTML Question

loop through list adding class by timer

I've got a small issue, I'm trying to create a type of carousel but slightly different, using jquery but at the moment I'm struggling to come up with the code. So far I've got a list:


What I want to do is create a loop, so add a active class to the first item in the list and for every 20 seconds remove it off the first item and then add the active class to the next one. Where this loop will then go back to the first item, after it hits the last item, if that makes sense.

So far I can identify the items within the list using this jquery but will the loop function go in here:

$( "ul li" ).each(function( index ) {
console.log( index + ": " + $( this ).text() );

jsg jsg
Answer Source

Figured it out if anyone else has this issue

var list = setInterval( function () {
            $( "ul li" ).removeClass("active");
            $("ul li" ).eq(current).addClass('active');
            if(current >= x) {
                current = 0;
        }, 14000);