react-native time定时器 防止内存泄露的注意点
来源:互联网 发布:excel数据监控软件 编辑:程序博客网 时间:2024/06/04 19:58
time
是react-native
提供的一个定时器,在实际使用中,经常会有使用不对,造成内存泄露的情况。很多React Native
应用发生致命错误(闪退)是与计时器有关。具体来说,是在某个组件被卸载(unmount)
之后,计时器却仍然在运行。
防止出问题的办法也很简单,在unmount
的时候,增加卸载定时器的操作:
componentDidMount() { this.timer = setTimeout( () => { console.log('把一个定时器的引用挂在this上'); }, 500 ); }componentWillUnmount() { // 请注意Un"m"ount的m是小写 // 如果存在this.timer,则使用clearTimeout清空。 // 如果你使用多个timer,那么用多个变量,或者用个数组来保存引用,然后逐个clear this.timer && clearTimeout(this.timer); }
这里的timer是在DidMount
中赋值的,如果是多次赋值呢?比如:
export default class test extends Component { render() { return ( <View style={styles.container}> <Button title={'click5000'} onPress={this.alertInfo.bind(this,5000)} /> <Button title={'click2000'} onPress={this.alertInfo.bind(this,2000)} /> <Button title={'clean timer'} onPress={this.cleanTimer.bind(this)} /> </View> ); } alertInfo(time){ this.timer = setTimeout( ()=>{ alert('hah'); }, time, ); } cleanTimer(){ this.timer && clearTimeout(this.timer); console.log('timer cleared') }}AppRegistry.registerComponent('test', () => test);
这里要介绍下setTimeout
的返回值,我们打断点可以看到,this.timer
是一个number。根据stack上面的其他网友的回答what-does-settimeout-return,setTimer会返回一个id,代表你已经向js的runtime系统中成功注册了一个定时器任务,这个id就是系统返回的id。
那如果是需要多次赋值,就一定要先将time clear掉,然后再赋值,或者使用多个参数来标志,否则之后就找不到上次的id,也就没办法clear了,同样可能造成内存泄露的情况。
例子如下:
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, Button} from 'react-native';export default class test extends Component { render() { return ( <View style={styles.container}> <Button title={'click5000'} onPress={this.alertInfo.bind(this,5000)} /> <Button title={'click2000'} onPress={this.alertInfo.bind(this,2000)} /> <Button title={'clean timer'} onPress={this.cleanTimer.bind(this)} /> </View> ); } alertInfo(time){ this.timer = setTimeout( ()=>{ alert('hah'); }, time, ); } cleanTimer(){ this.timer && clearTimeout(this.timer); console.log('timer cleared') }}const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, welcome: { fontSize: 20, textAlign: 'center', margin: 10, }, instructions: { textAlign: 'center', color: '#333333', marginBottom: 5, },});AppRegistry.registerComponent('test', () => test);
这里的timer多次赋值,虽然前面的值被后面的覆盖,但是前面的time仍然会起效果,如果不想前面的time work,需要clear掉,然后再赋值。
代码如下:
alertInfo(time){ this.timer && clearTimeout(this.timer); this.timer = setTimeout( ()=>{ alert('hah'); }, time, ); }
综上,使用time的注意点:
- 记得unmount的时候,clear
- 多个timer要使用多个变量或者数组
- 多次赋值,记得把之前的值clear掉
阅读全文
0 0
- react-native time定时器 防止内存泄露的注意点
- C++内存泄露注意点
- C++内存泄露注意点
- react native 定时器的使用
- React Native 热更新 注意点1
- react native 打包APK 注意点
- react native 中DataPickerAndroid的用法和一些注意注意点
- 定时器造成的内存泄露
- React Native -19.React Native Timer定时器的使用
- iOS ARC下内存泄露 注意点
- React Native中定时器的使用
- react-native 定时器
- [Android-内存]android应用java编程内存泄露注意点
- 防止C代码内存泄露的方法
- 防止android应用的内存泄露
- 防止内存泄露的一些Tips
- 防止android应用的内存泄露
- 防止 Java 内存泄露的安全措施
- C的多态
- c++类和对象二
- Android-贪吃蛇(三)- Mainactivity
- 剑指offer题解C++【12】数值的整数次幂
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- react-native time定时器 防止内存泄露的注意点
- java的CSV读写
- 6147问题
- 插入USB设备,无法在windows下ubuntu中找到设备节点的问题解决
- android 效果网址
- 代码适用性-低耦合案例
- 写hql时,路径中有"\"时,要将它换成"\\",才能在数据库中显示"\"
- 使用create-react-app自动构建react项目
- Intellig IDEA使用log4j打印log