Question about changing the interval speed on loops

Is is possible to change the interval speed on loops? I’m not sure if it is because I’ve trying to figure it out for 9 hours now. I don’t need the answer, I’m just wondering if you can chang the int speed for loops in javascript

I dont think thats a thing, or at least I have never heard or seen any do it before. Why do you want to slow down the speed of a loop?

Basically im trying to make a memory game where the computer repeats its values, and it gets progressively faster. I’m trying to get the first part down, and i think i figured it out the best i could. This is what I have so far:

let gameDecision = "Computer phase";
const buttonArray =  JSON.parse(localStorage.getItem("buttonArray")) || [];
const playerButtonArray = [];
const redButton = document.getElementById("red");
const greenButton = document.getElementById("green");
const blueButton = document.getElementById("blue");
const orangeButton = document.getElementById("orange");

function buttons(set){
    redButton.addEventListener("click", e =>{
     playerButtonArray.push(0)
    })
    greenButton.addEventListener("click", e =>{
        playerButtonArray.push(1)
    })
    blueButton.addEventListener("click", e =>{
        playerButtonArray.push(2)
    })
    orangeButton.addEventListener("click", e =>{
        playerButtonArray.push(3)
    })
}
buttons()

function updateLocalStorage(){
    localStorage.setItem("buttonArray", JSON.stringify(buttonArray));
}

function update(colorButton, oldColor, newColor){
    setTimeout(() =>{
        colorButton.style.backgroundColor = oldColor;
        setTimeout(() => {
            colorButton.style.backgroundColor = newColor;
        }, 500)
    }, 500 )
}

function promises(timerSpeed){
    if(gameDecision = "Computer phase"){
    let counter = 0;
const theInterval = setInterval(() =>{
console.log(buttonArray[counter++]);
if(counter > buttonArray.length){
    gameDecision = "Player state";
    clearInterval(theInterval)
}
if(counter === 0){
    update(redButton, "darkred", "red");
} else if(counter === 1){
    update(greenButton, "darkgreen", "#4CAF50");
} else if(counter === 2){
    update(blueButton, "darkblue", "rgba(0, 102, 255, 0.904)");
} else if(counter === 3){
    update(orangeButton, "rgb(255, 102, 0)", "orange");
}
}, 2000)
    }
}
promises()

Also I’m now trying to figure out how I can get every instance of the counter showing 0 to light up the buttons but it looks like it’s only doing it once. So, I would also like some help with that if there’s any array properties that can accomplish this.

So, It looks like my values that i’m using indexOf for are duplicating themselves in the array. I’m not too sure why it’s doing this, and any help is appreciated.

function instances(items, set){
    const varArray = [];
    for(let i = 0; i < buttonArray.length; i++){
        set = buttonArray.indexOf(items, set + 1)
            varArray.push(set)
    }
    console.log(varArray)
console.log(buttonArray)
}
instances(1)
let gameDecision = "Computer phase";
const buttonArray =  JSON.parse(localStorage.getItem("buttonArray")) || [];
const playerButtonArray = [];
const redButton = document.getElementById("red");
const greenButton = document.getElementById("green");
const blueButton = document.getElementById("blue");
const orangeButton = document.getElementById("orange");

function buttons(set){
    redButton.addEventListener("click", e =>{
     playerButtonArray.push(0)
    })
    greenButton.addEventListener("click", e =>{
        playerButtonArray.push(1)
    })
    blueButton.addEventListener("click", e =>{
        playerButtonArray.push(2)
    })
    orangeButton.addEventListener("click", e =>{
        playerButtonArray.push(3)
    })
    
}
buttons()

function updateLocalStorage(){
    localStorage.setItem("buttonArray", JSON.stringify(buttonArray));
}

function update(colorButton, oldColor, newColor){
    setTimeout(() =>{
        colorButton.style.backgroundColor = oldColor;
        setTimeout(() => {
            colorButton.style.backgroundColor = newColor;
        }, 500)
    }, 500 )
}


function instances(items, set){
    const varArray = [];
    for(let i = 0; i < buttonArray.length; i++){
        set = buttonArray.indexOf(items, set + 1)
            varArray.push(set)
    }
    console.log(varArray)
console.log(buttonArray)
}
instances(1)
function promises(){
    if(gameDecision = "Computer phase"){
        updateLocalStorage()
    let counter = 0;
const theInterval = setInterval(() =>{
buttonArray[counter++];
if(counter > buttonArray.length){
    clearInterval(theInterval)
}

}, 1000)
    }
}
promises()

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.