function animate() { $('.animated').each(function () { var current = $(this).find('.active').removeClass('active'); next = current.next().length ? current.next() : $(this).children().eq(0); next.addClass('active'); }); setTimeout(animate, 1500); } if ($('.animated').length) { animate(); }