Countdown timer in React

后端 未结 7 811
甜味超标
甜味超标 2020-11-30 00:05

I have seen lots of countdown timers in JavaScript and wanted to get one working in React.

I have borrowed this function I found online:

secondsToTim         


        
7条回答
  •  自闭症患者
    2020-11-30 00:36

    Basic idea showing counting down using Date.now() instead of subtracting one which will drift over time.

    class Example extends React.Component {
      constructor() {
        super();
        this.state = {
          time: {
            hours: 0,
            minutes: 0,
            seconds: 0,
            milliseconds: 0,
          },
          duration: 2 * 60 * 1000,
          timer: null
        };
        this.startTimer = this.start.bind(this);
      }
    
      msToTime(duration) {
        let milliseconds = parseInt((duration % 1000));
        let seconds = Math.floor((duration / 1000) % 60);
        let minutes = Math.floor((duration / (1000 * 60)) % 60);
        let hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
    
        hours = hours.toString().padStart(2, '0');
        minutes = minutes.toString().padStart(2, '0');
        seconds = seconds.toString().padStart(2, '0');
        milliseconds = milliseconds.toString().padStart(3, '0');
    
        return {
          hours,
          minutes,
          seconds,
          milliseconds
        };
      }
    
      componentDidMount() {}
    
      start() {
        if (!this.state.timer) {
          this.state.startTime = Date.now();
          this.timer = window.setInterval(() => this.run(), 10);
        }
      }
    
      run() {
        const diff = Date.now() - this.state.startTime;
        
        // If you want to count up
        // this.setState(() => ({
        //  time: this.msToTime(diff)
        // }));
        
        // count down
        let remaining = this.state.duration - diff;
        if (remaining < 0) {
          remaining = 0;
        }
        this.setState(() => ({
          time: this.msToTime(remaining)
        }));
        if (remaining === 0) {
          window.clearTimeout(this.timer);
          this.timer = null;
        }
      }
    
      render() {
        return ( <
          div >
          <
          button onClick = {
            this.startTimer
          } > Start < /button> {
            this.state.time.hours
          }: {
            this.state.time.minutes
          }: {
            this.state.time.seconds
          }. {
            this.state.time.milliseconds
          }:
          <
          /div>
        );
      }
    }
    
    ReactDOM.render( < Example / > , document.getElementById('View'));
    
    
    

提交回复
热议问题