在流星中使用setInterval与React

杰夫·弗雷泽(Jeff Frazier)

我正在尝试了解如何将Meteor中的setInterval或类似内容与React用作计时器。我有一个具有每小时开始和结束时间的子组件,并使用moment.js来获取当前时间。如果当前时间在开始时间和结束时间之间,则显示进度条。

我正在使用react-timer-mixin,现在我的组件看起来像这样。

Driver = React.createClass({
  mixins: [TimerMixin],
  componentDidMount: function(){
    // componentDidMount is called by react when the component
    // has been rendered on the page. We can set the interval here:
    this.setInterval(this.currentTime, 15000);
  },

  currentTime: function() {
    //  Get the start time.
    var beginTime = moment(this.props.driver.startTime,"hh:mm");
    // Add an hour for end time.
    var endTime = moment(beginTime).add(1,'hours');
    // Get the current time.
    var now = moment();
    // Get total minutes between start and end.
    totalMin = endTime.diff(beginTime);
    // Get elapsed minutes.
    currMin = now.diff(beginTime);
    // Determine where we are in the schedule.
    if (moment(now).isBetween(beginTime, endTime)) {
      progress = Math.round((currMin / totalMin) * 60, -1);
      console.log(progress);
      return progress;
    }
    else {
      // Show this schedule as done.
      return 60
    }
  }, 

  // A bunch of other functions

  render() {
    return (
      <DriverBar current={this.currentTime()} total="60" />
    );
  }
});

我确定currentTime函数正在setInterval内运行,因为浏览器控制台日志中的进度每15秒更新一次,因为此刻我的函数中已有进度。但是,我无法在组件中获取更新的进度值。它显示初始进度值,但不使用setInterval更新。我只是在打错电话<DriverBar />吗?

另外,如果这不是React或Meteor的方式,那么我绝对不会坚持下去,并且会感激指导。

杰夫·弗雷泽(Jeff Frazier)

这种情况的答案是使用状态。非常感谢Ramsay Lanier的帮助。

Driver = React.createClass({
  mixins: [TimerMixin],
  getInitialState: function() {
    return {progress: 0};
  },

  componentDidMount() {
    this.setInterval(this.currentTime, 1000);
  },

  currentTime: function() {
    [...]

    if (moment(now).isBetween(beginTime, endTime)) {
      progress = Math.round((currMin / totalMin) * 60, -1);
      this.setState({progress: progress});
    }
  },

render() {
  let progress = this.state.progress;

  return (
    <DriverBar current={progress} total="60" />
  );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在React中使用setinterval

来自分类Dev

如何在 React.js 中使用 componentWillUnmount 删除 setInterval

来自分类Dev

在流星包中使用npm

来自分类Dev

在流星中使用“ mousemove”事件

来自分类Dev

在流星中使用“ mousemove”事件

来自分类Dev

在流星中使用this.add

来自分类Dev

流星setInterval堆叠

来自分类Dev

流星/翡翠:在模板ID中使用变量

来自分类Dev

如何在Bootbox中使用流星模板?

来自分类Dev

如何在流星中使用ActiveMQ?

来自分类Dev

在流星中使用Crypto.js

来自分类Dev

在流星中使用Session时应用崩溃

来自分类Dev

如何在流星中使用nodejs包

来自分类Dev

在Modernizr中使用流星的正确方法

来自分类Dev

在流星中使用订阅的正确方法

来自分类Dev

在外部框架中使用流星功能

来自分类Dev

流星:在子模板中使用变量

来自分类Dev

在React中的useEffect()钩子中使用setInterval()之前尝试进行API调用

来自分类Dev

我总是会收到错误消息,指出未为流星1.3和React中使用的jquery插件定义功能

来自分类Dev

我总是会收到错误消息,指出未为流星1.3和React中使用的jquery插件定义功能

来自分类Dev

在ObjectiveC的JavascriptCore框架中使用setInterval,setTimeout

来自分类Dev

如何在反应中使用setInterval?

来自分类Dev

在ObjectiveC的JavascriptCore框架中使用setInterval,setTimeout

来自分类Dev

在循环中使用setInterval更改间隔

来自分类Dev

在 JavaScript 中使用 setInterval 设置动画

来自分类Dev

如何在流星项目中使用JSplumb(流星项目版本0.6.5.1)

来自分类Dev

如何在流星中使用非npm node_module?

来自分类Dev

流星,在mongodb中使用正则表达式发布

来自分类Dev

流星:如何在“车把”中使用“真棒字体”?

Related 相关文章

热门标签

归档