ReactNative中的定时器

定时器

  • 延时:setTimeout, clearTimeout
  • 定时器:setInterval, clearInterval
import React,{
  Component
} from 'react';

export default class Hello extends Component {
  componentDidMount() {
    this.timer = setTimeout(
      () => { console.log('把一个定时器的引用挂在this上'); },
      500
    );
  }
  componentWillUnmount() {
    // 请注意Un"m"ount的m是小写

    // 如果存在this.timer,则使用clearTimeout清空。
    // 如果你使用多个timer,那么用多个变量,或者用个数组来保存引用,然后逐个clear
    this.timer && clearTimeout(this.timer);
  }
};

猜你喜欢

转载自blog.csdn.net/hejiasu/article/details/78995290
今日推荐