<div class = "elem"></div>
<div class = "elem"></div>
let b = document.querySelectorAll('.elem');
for (var i = 0; i < b.length; i++){
setTimeout(function(){
b[i].style.backgroundColor= "red";
},1000)
}
const list = document.querySelectorAll('.elem');
let pool = [...list];
let counter = 0;
animate();
function animate(index = 0) {
setTimeout(() => {
if (!pool.length) {
pool = [...list];
counter = 0;
}
const current = pool.shift();
list[index - 1] && (list[index - 1].style.backgroundColor = 'white');
current.style.backgroundColor = 'red';
animate(++counter);
}, 1000);
}