I have built a rock/paper/scissors React app. The app is working fine, I just would like to add some delay when the CPU has to choose the weapon(rock/paper/scissors).
I would like that there is a window of time where the image selected by the CPU is not on the screen, while the user's choice is immediately appearing on screen.
I tried to add a setInterval() function inside my compounentDidMount() method but with no luck.
How can I add the delay only on the CPU part ?
https://codesandbox.io/s/nice-ardinghelli-96sum?file=/src/components/Main.js
Thank you very much in advance.
Main.js
import React from "react"
import Choice from "./Choice"
import TryAgain from "./TryAgain"
import paper from '../images/icon-paper.svg'
import rock from '../images/icon-rock.svg'
import scissors from '../images/icon-scissors.svg'
import './Main.css';
class Main extends React.Component {
constructor(props) {
super(props)
this.state = {
onScreen: true,
choiceName: '',
choiceImage: '',
choiceBorderColor: '',
choiceExtraBorderColor: '',
houseChoice: '',
results:'',
}
this.handleClick = this.handleClick.bind(this)
this.handleTryAgainClick = this.handleTryAgainClick.bind(this)
}
/******************* setInterval() ******************************/
componentDidMount() {
// Call this function so that it fetch first time right after mounting the component
this.handleClick();
// set Interval
this.interval = setInterval(this.handleClick, 5000);
}
/*function that handles the user choice*/
handleClick = (choiceName, choiceImage, choiceBorderColor, choiceExtraBorderColor) => () => {
this.setState({
onScreen: false,
choiceName,
choiceImage,
choiceBorderColor,
choiceExtraBorderColor,
})
/*function that get a random number between 0 and 2, and set this number to the house index*/
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
const index = getRandomInt(3)
this.setState({
houseChoice: index
})
const results = this.getResults(choiceName, index).toUpperCase()
this.setState({
results: results,
})
if(results === "WIN") {
this.props.setScore(1)
} else if (results === "LOSE") {
this.props.setScore(-1)
}
else {
this.props.setScore(0)
}
}
/*function that get the main logic and the results of the game*/
getResults(choiceName, houseChoice) {
if(choiceName === "paper" && houseChoice === 0) {
return "draw"
} else if(choiceName === "paper" && houseChoice === 1) {
return "lose"
} else if(choiceName === "paper" && houseChoice === 2) {
return "win"
}
if(choiceName === "rock" && houseChoice === 0) {
return "lose"
} else if(choiceName === "rock" && houseChoice === 1) {
return "win"
} else if(choiceName === "rock" && houseChoice === 2) {
return "draw"
}
if(choiceName === "scissors" && houseChoice === 0) {
return "win"
} else if(choiceName === "scissors" && houseChoice === 1) {
return "draw"
} else if(choiceName === "scissors" && houseChoice === 2) {
return "lose"
}
}
/*function that switches the screen and resets the index of the house*/
handleTryAgainClick() {
this.setState({
onScreen: true,
houseChoice: ''
})
}


setTimeoutbecausesetIntervaldoing something entirely different.... So read about conditional rendering. Your question is not minimal so Im not going to get over all this code for answering, hope you will get an answer.