React 中的倒数计时器 [英] Countdown timer in React

查看:29
本文介绍了React 中的倒数计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JavaScript 中看到了很多倒数计时器,并希望让一个在 React 中工作.

我借用了我在网上找到的这个功能:

secondsToTime(secs){让小时 = Math.floor(secs/(60 * 60));让 divisor_for_minutes = secs % (60 * 60);让分钟 = Math.floor(divisor_for_minutes/60);让 divisor_for_seconds = divisor_for_minutes % 60;让秒 = Math.ceil(divisor_for_seconds);让 obj = {h":小时,m":分钟,s":秒};返回对象;};

然后我自己写了这段代码

initialTimer = () =>{让 timeLeftVar = this.secondsToTime(60);this.setState({ timeLeft: timeLeftVar })};startTimer = () =>{让间隔 = setInterval(this.timer, 1000);this.setState({间隔:间隔});};计时器 = () =>{如果(this.state.timeLeft > 0){this.setState({ timeLeft: this.state.timeLeft -1 });}别的 {clearInterval(this.state.interval);//this.postToSlack();}};

当前 onclick 会将屏幕上的时间设置为:Time Remaining: 1 m : 0 s但它并没有将其减少到 Time Remaining: 0 m : 59 s 然后是 Time Remaining: 0 m : 58 s etc etc

我想我需要使用不同的参数再次调用该函数.我该怎么做?

我忘了说,我想要这个功能,以便我可以使用几秒到几分钟秒

解决方案

你必须每秒setState 剩余的秒数(每次调用间隔时).举个例子:

class Example extends React.Component {构造函数(){极好的();this.state = { time: {}, seconds: 5 };this.timer = 0;this.startTimer = this.startTimer.bind(this);this.countDown = this.countDown.bind(this);}秒到时间(秒){让小时 = Math.floor(secs/(60 * 60));让 divisor_for_minutes = secs % (60 * 60);让分钟 = Math.floor(divisor_for_minutes/60);让 divisor_for_seconds = divisor_for_minutes % 60;让秒 = Math.ceil(divisor_for_seconds);让 obj = {h":小时,m":分钟,s":秒};返回对象;}componentDidMount() {让 timeLeftVar = this.secondsToTime(this.state.seconds);this.setState({ time: timeLeftVar });}开始定时器(){如果 (this.timer == 0 && this.state.seconds > 0) {this.timer = setInterval(this.countDown, 1000);}}倒数() {//删除一秒钟,设置状态以便重新渲染发生.让秒 = this.state.seconds - 1;this.setState({时间:this.secondsToTime(seconds),秒:秒,});//检查我们是否为零.如果(秒 == 0){clearInterval(this.timer);}}使成为() {返回(<div><button onClick={this.startTimer}>Start</button>米:{this.state.time.m} s:{this.state.time.s}

);}}ReactDOM.render(, document.getElementById('View'));

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><div id="查看"></div>

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:

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;
  };

And then I have written this code myself

  initiateTimer = () => {
    let timeLeftVar = this.secondsToTime(60);
    this.setState({ timeLeft: timeLeftVar })
  };

  startTimer = () => {
    let interval = setInterval(this.timer, 1000);
    this.setState({ interval: interval });
  };

  timer = () => {
    if (this.state.timeLeft >0){
      this.setState({ timeLeft: this.state.timeLeft -1 });
    }
    else {
      clearInterval(this.state.interval);
      //this.postToSlack();
    }
  };

Currently onclick it will set the time on screen to: Time Remaining: 1 m : 0 s But it does not reduce it to Time Remaining: 0 m : 59 s and then Time Remaining: 0 m : 58 s etc etc

I think I need to call the function again with a different parameter. how can I go about doing this ?

Edit: I forgot to say, I would like the functionality so that I can use seconds to minutes & seconds

解决方案

You have to setState every second with the seconds remaining (every time the interval is called). Here's an example:

class Example extends React.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);
    }
  }

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

ReactDOM.render(<Example/>, document.getElementById('View'));

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="View"></div>

这篇关于React 中的倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆