I have a function that changes the colour of an element briefly, then changes it back to the original colour. Depending on the level you're at (it's a puzzle game), a forEach loop then runs this function for a a certain number of times (more at higher levels). Currently the colour the element changes to is whatever I've manually input into the code. I'm trying to find a way to changing that colour every time the forEach runs the function.
For example, say you are on the first round and the forEach runs three times, the element will flash red-white, red-white, red-white. What I need is for it to flash red-white, blue-white, pink-white. It also needs to loop back to the beginning of the array when it runs out of colours. For example at a higher level the forEach might run 6 times, so the iteration over the colours array will have to go back to the start once. Here's the code:
function showCircle(item, j, x) {
setTimeout(function () {
let x = 0;
let colors = ['blue','pink','green']
let color = colors[x];
var num = initArray[j];
var element = document.getElementById(num)
element.classList.add(`cell-glow-${color}`)
window.setTimeout(function () {
element.classList.remove(`cell-glow-${color}`)
}, 400);
j++;
x++
console.log(color)
}, speed() * j);
};
function showEachCircle(captureUserClicks) {
initArray.forEach(showCircle);
}
Clearly what's happening above is that the showCircle function is zero-ing x each time, so it gets stuck on the first iteration. But I'm not sure where I should be putting those variables to make it iterate properly. Plus I haven't even begun to get my head around forcing the array to go back to the beginning.
Any ideas? Thank you!