Using setInterval in Meteor with a reaction

I am trying to figure out how to use setInterval or the like in Meteor with React for a timer. I have a child component that has an hour start and end time, and uses moment.js to get the current time. If the current time is between the start and end time, I show a progress bar.

I am using react-timer-mixin and now my component is as follows.

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 />?

, , , .

+4
2

- . ​​.

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" />
  );
}
0

, this. , this, , , setTimer. javascript bind this .

:

this.setInterval(this.currentTime.bind(this), 15000);
+1

Source: https://habr.com/ru/post/1627324/


All Articles