Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add and remove a class in jquery every 4 seconds

for some reason, this isn't adding and removing a new class on elements with the class of post, every 4 seconds. jquery loads correctly, as does this. chrome shows no errors with the code.

$(document).ready(function(){
    $('.post').addClass('display').delay(4000).removeClass('display');
});
like image 291
Kegan Quimby Avatar asked Nov 27 '22 07:11

Kegan Quimby


1 Answers

Since you listed you want this to happen every 4 seconds you can simply use setInterval()

var $post = $(".post");
setInterval(function(){
    $post.toggleClass("display");
}, 4000);

Note, the selector is cached in $post to minimize the number of times the DOM needs queried on each interval.

Example on jsfiddle

like image 146
Mark Coleman Avatar answered Dec 09 '22 17:12

Mark Coleman