React Native -19.React Native Timer定时器的使用
React Native -19.React Native Timer定时器的使用
Step1:介绍RN的定时器就是一个创建方法。并没有像iOS一样的NSTimer类
Step1:使用
Step2:实战import React,{Component} from 'react';
import {AppRegistry,StyleSheet,ActivityIndicator} from 'react-native';
class hello extends Component {
constructor(props:any){
super(props);
var timer1=null;
var timer2=null;
var timer3=null;
this.state = {
animating: true,};
}
componentDidMount(){
this.timer1 = setInterval(
()=>{
this._consolelogshow();
},2000,);
this.timer2 = setTimeout(
()=>{console.log('setTimeout22222222'); },1000,);
this.timer3 = setImmediate(
()=>{console.log('setImmediate333333');},3000,);
}
componentWillUnmount() {
// 如果存在this.timer,则使用clearTimeout清空。
// 如果你使用多个timer,那么用多个变量,或者用个数组来保存引用,然后逐个clear
this.timer1 && clearInterval(this.timer1);
this.timer2 && clearTimeout(this.timer2);
this.timer3 && clearImmediate(this.timer3);
}
_consolelogshow(){
console.log('把一个定时器的引用挂在this上22222');
}
render(){
return(
<ActivityIndicator
animating={this.state.animating}
style={[styles.centerting,{height:80}]}
size="large"/>
)
}
}
var styles = StyleSheet.create({
centering: {
alignItems: 'center',justifyContent: 'center',padding: 8,}
});
AppRegistry.registerComponent('hello',()=>hello);
Step3:实战解读
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |