React Native中定时器的使用

来源:互联网 发布:snapgene mac 编辑:程序博客网 时间:2024/05/25 20:00

React Native中定时器的使用

React Native中定时器的使用分为ES5和ES6两种,前者使用较为麻烦

ES5中使用方式

  1. 引入计时器类库
var TimerMixin = require('react-timer-mixin');
  1. 注册计时器
mixins: [TimerMixin],
  1. 添加定时器
    this.timer = this.setInterval(function() {    }, 3000)

注意定时器开启就得有清除,一般在componentWillUnMount调用,图形卸载时清楚定时器也好理解

componentWillUnMount() {    this.setInterval && clearInterval(this.setInterval)  }

ES6中使用方式

很简单

this.timer = setTimeout(function() {      alert('3秒后弹出')    }, 3000);

同样定时器开启就得有清除

this.timer && clearTimeout(this.timer)
0 0