您的位置:首页 > Web前端 > React

React Native中定时器的使用

2016-12-28 23:44 579 查看

React Native中定时器的使用

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

ES5中使用方式

引入计时器类库

var TimerMixin = require('react-timer-mixin');


注册计时器

mixins: [TimerMixin],


添加定时器

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  native RN定时器