what’s the best way to add a class to the div with a class of post every 4 seconds using jquery?

what’s the best way to add a class to the div with a class of post every 4 seconds using jquery?

<div class="34 post">
<img width="311" height="417" src="#" class="#" alt="newspapers" />
<h2><a href="#">Headline News Part 2</a></h2>
<p>testing new content</p>
</div>

<div class="9 post">
<img width="311" height="417" src="#" class="#" alt="newspapers" />
<h2><a href="#">Headline News Part 2</a></h2>
<p>testing new content</p>
</div>

<div class="6 post">
<img width="311" height="417" src="#" class="#" alt="newspapers" />
<h2><a href="#">Headline News Part 2</a></h2>
<p>testing new content</p>
</div>

so i want the first to have a class of “display” then after 4 seconds, i want to remove the class on that one and add it to the second one. and then after 4 more seconds, remove it from the second and add it to the third. when it gets to the end it loops back around.

Related posts

Leave a Reply

1 comment

  1. var post_count = $('.post').length,
        current_index = 0;
    
    setInterval(function(){
    
      $('.post').removeClass('display').eq(current_index).addClass('display');
    
      current_index++;
    
      if(current_index > post_count)
        current_index = 0; 
    
    }, 4000);
    

    Wrap it in a jQuery document.ready function 🙂