react 计时器如何实现定时任务来从数据库去数据并放在state中

我现在有赢组件,是从数据库中取得数据,并通过state状态存放

callGetMvnDataPackageLogFn(orderCodeData).then((res) => {      this.setState({ data: res.data });
    });

我现在想要给这个添加一个计时器去定时请求数据,我自己使用的是:

const intervals = stInterval(function(){
  callGetMvnDataPackageLogFn(orderCodeData).then((res) => {
    this.setState({ data: res.data });
  });},200)

但是这并不行,请问该如何写?


德玛西亚99
浏览 1258回答 1
1回答

Cats萌萌

React官网首页的定时器例子,自己琢磨下!class Timer extends React.Component {&nbsp; constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = {secondsElapsed: 0};&nbsp; }&nbsp; tick() {&nbsp; &nbsp; this.setState((prevState) => ({&nbsp; &nbsp; &nbsp; secondsElapsed: prevState.secondsElapsed + 1&nbsp; &nbsp; }));&nbsp; }&nbsp; componentDidMount() {&nbsp; &nbsp; this.interval = setInterval(() => this.tick(), 1000);&nbsp; }&nbsp; componentWillUnmount() {&nbsp; &nbsp; clearInterval(this.interval);&nbsp; }&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div>Seconds Elapsed: {this.state.secondsElapsed}</div>&nbsp; &nbsp; );&nbsp; }}ReactDOM.render(<Timer />, mountNode);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript