SetTimer when it reaches 0seconds it automatically restarts in reactjs

Jun 30 2021 9:13 AM

I want to restart the timer when it reaches 0 seconds. It again starts with 5seconds. here is my code. Actually, I want the timer to start at 9.00 AM "23:59:59" next day morning 9.00 AM it restarts with 23:59:59 continuously. Please share your idea.

import React , {Component} from 'react'


class PostList extends Component{

    
    constructor() {
        super();
        this.state = { time: {}, seconds: 5 };
        this.timer = 0;
        this.startTimer = this.startTimer.bind(this);
        this.countDown = this.countDown.bind(this);
      }
    
      secondsToTime(secs){
        let hours = Math.floor(secs / (60 * 60));
    
        let divisor_for_minutes = secs % (60 * 60);
        let minutes = Math.floor(divisor_for_minutes / 60);
    
        let divisor_for_seconds = divisor_for_minutes % 60;
        let seconds = Math.ceil(divisor_for_seconds);
    
        let obj = {
          "h": hours,
          "m": minutes,
          "s": seconds
        };
        return obj;
      }
    
      componentDidMount() {
        let timeLeftVar = this.secondsToTime(this.state.seconds);
        this.setState({ time: timeLeftVar });
      }
    
      startTimer() {
        if (this.timer == 0 && this.state.seconds > 0) {
          this.timer = setInterval(this.countDown, 1000);
        }
      }
    
      countDown() {
        // Remove one second, set state so a re-render happens.
        let seconds = this.state.seconds - 1;
        this.setState({
          time: this.secondsToTime(seconds),
          seconds: seconds,
        });
        
       

        // Check if we're at zero.
        if (seconds == 0) { 
          clearInterval(this.timer);
          //this.updateTimer = setInterval(() => this.startTimer, 1000);
          this.startTimer();
        }


        
      }
    
      render() {
        return(
          <div>
            <button onClick={this.startTimer}>Start</button>
            m: {this.state.time.m} s: {this.state.time.s}
          </div>
        );
      }
    }
export default PostList