我是靠谱客的博主 优秀鱼,这篇文章主要介绍react 中使用定时器 Timers(定时器),现在分享给大家,希望可以做个参考。

  • setTimeout,clearTmeout
  • setInterval,clearInterval

在 class 中

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class TimersDemo extends Component { constructor(props) { super(props); this.state={ content:'', } } componentDidMount() { this.timer = setTimeout( () => { this.setState({content:'我是定时器打印的内容...One'}) }, 500 ); this.timer_two = setTimeout( () => { this.setState({msg:'我是定时器打印的内容...Two'}) }, 1000 ); } componentWillUnmount() { this.timer && clearTimeout(this.timer); this.timer_two && clearTimeout(this.timer_two); } render() { return ( <View style={{margin:20}}> <Text style={styles.welcome}> 定时器实例 </Text> <Text>{this.state.content}</Text> <Text>{this.state.msg}</Text> </View> ); } }
复制代码
1
2
setTimeout 延时的定时执行



复制代码
1
2
3
4
5
6
7
8
9
10
11
<CustomButton text='测试setInterval' onPress={()=>{ this.interval=setInterval(() => {this.setState({sum:(this.state.sum+1)}); },400); }} /> <CustomButton text='clearInterval' onPress={()=>{ this.interval && clearInterval(this.interval); }} />

 

复制代码
1
2
setInterval 定时间隔执行
复制代码
1
 

转载于:https://www.cnblogs.com/jshare/p/7778453.html

最后

以上就是优秀鱼最近收集整理的关于react 中使用定时器 Timers(定时器)的全部内容,更多相关react内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部