Update status by observing date change

1
Hello, I am making a component that shows the current date, but I would like it to update as soon as the change occurred, but I was only able to leave it active for a change with a timeout, which I think is not a good solution, all the time unnecessarily or I would have to adjust a high value which could cause a delay at the time of the update.

this.state = {
            curDate : null
        }

componentDidMount() {
    setInterval( () => {
        this.setState({
            curDate : new Date().getSeconds()
         })
    },1000)
}

render() {
     return (
         <div>
             <p>{this.state.curDate}</p>
         </div>
      );
}
    
asked by anonymous 20.03.2018 / 20:09

1 answer

0

I do not think you have much choice in using an interval. What you can do to decrease the number of executions (including you do not have so many features like that, so you do not have to worry) is in the load of your application, make count how much time is left for the day to turn. Then you put in a timeout with the right time and in theory you only need to run once a day. When the date turns you put another timeout for 24h and so on.

    
30.04.2018 / 13:36