it´s supposed to change the id of a div, wait half a second, and then change the next div's id. i want it to loop only after removing the id from the previous div.
for(var i=0; i<sequence.length;i++){
var qS = $("#gamearea .bsq:nth-child("+sequence[i]+")")
qS.attr({
id:"acende"
})
setTimeout(function(){
qS.removeAttr("id")
},500);
}
}
Sounds like an X/Y problem WHY would you want to change the ID? If you want to change colour, add and remove a CLASS –
const sequence = [1, 4, 3, 0, 2]; // 0 based
const $qS = $("#gamearea .bsq");
const len = $qS.length;
let cnt = 0;
setInterval(() => {
$qS.removeClass("active");
$qS.eq(sequence[cnt]).addClass("active");
cnt++;
if (cnt >= len) cnt = 0;
}, 500)
.active {
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="gamearea">
<div class="bsq">1</div>
<div class="bsq">2</div>
<div class="bsq">3</div>
<div class="bsq">4</div>
<div class="bsq">5</div>
</div>