function toggleClass(selector, className, delay) {
let index = -1;
return setInterval($items => {
$items.eq(index).removeClass(className);
index = (index + 1) % $items.length;
$items.eq(index).addClass(className);
}, delay, $(selector));
}
const intervalId = toggleClass('.wrap div', 'active', 300);
или
function toggleClass(selector, className, delay) {
const items = document.querySelectorAll(selector);
let index = ~-items.length;
return items.length
? setInterval(() => {
items[index].classList.remove(className);
index = -~index % items.length;
items[index].classList.add(className);
}, delay)
: null;
}