I am trying to have the class of each elements change one at a time in sequence automatically. This means element 1 glows then goes off as element 2 glows and then goes off and so on. When each element has glowed once the whole sequence starts over.
$('header div:first').toggleClass('highlight').nextAll().toggleClass('none');
function highlight() {
var $off = $('header div.highlight').toggleClass('none');
if ($off.next().length) {
$off.next().toggleClass('none');
} else {
$off.prevAll().last().toggleClass('highlight');
}
}
$(document).ready(function() {
setInterval(highlight, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<div>element 1</div>
<div>element 2</div>
<div>element 3</div>
<div>element 4</div>
</header>
It wont work as expected (elements 2 through 4 highlight all at the same time and then go off while element 1 doesnt change at all) and I dont know why. What am I doing wrong?
.none
is useless. All you need is your.highlight
– Pieperhighlight
class instead. – Duodenary.queue()
,.promise()
– Enriqueenriqueta