sahdg
sahdg

Reputation: 3

Reactjs argument expression expected in setinterval

I am using setInterval in reactjs but as soon as I use if expression in it. I got an error. My code is

    setInterval (
        if(this.state.count >4){
            this.setState({
                classcolor: colors[Math.floor((Math.random() * 2) + 0)]
            })
        }
        this.setState(prevState => ({
            count: prevState.count + 1
        }), () => {this.setState({
            classcolor: colors[Math.floor((Math.random() * 2) + 0)]
        })})
        , 2000)

How can I solve this problem? Can someone please help me in this.

Upvotes: 0

Views: 161

Answers (1)

Jayce444
Jayce444

Reputation: 9073

The first argument of setInterval should be a function. You've just launched straight into the function body code. That code needs to be inside a function, like so:

setInterval (() => {
  if(this.state.count >4){
      this.setState({
          classcolor: colors[Math.floor((Math.random() * 2) + 0)]
      })
  }
  this.setState(prevState => ({
      count: prevState.count + 1
  }), () => {this.setState({
      classcolor: colors[Math.floor((Math.random() * 2) + 0)]
  })})
}, 2000);

Upvotes: 2

Related Questions