I want to change the name of a class every 2 seconds, based on a list of names (var "items").
I found a pretty good SO here and tried to change it to my needs. It now works for me, but I don't know, how to remove $text and the div "#div1" - because I don't need it. In the other SO it was used to change the html text of the div. But I don't need this, I only want to change the class name of the section - I tried to remove this code, but then it do not work anymore.
HTML:
<section class="steps">class name of this section changes (see inspector)</section>
<div id="div1"></div> <!-- this could be removed -->
<button>Stop it Now.</button>
JS:
$(document).ready(function() {
var items = ["one", "two", "three"],
$text = $( '#div1' ),
$step = $( 'section.steps' ),
delay = 2; //seconds
function loop ( delay ) {
$.each( items, function ( i, elm ){
$text.delay( delay*1E3).hide();
$text.queue(function(){
$text.html( items[i] );
$text.dequeue(); // this is not needed
$step.addClass( items[i] ).delay(2000).queue(function(){
$(this).removeClass( items[i] ).dequeue();
});
});
$text.show();
$text.queue(function(){
if ( i == items.length -1 ) {
loop(delay);
}
$text.dequeue();
});
});
}
loop( delay );
$("button").on("click", function() {
$text.stop(true, false);
})
});
$text.remove();