RN中的定时器



import React, { Component } from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    View,
    Image,
    ScrollView
} from 'react-native';

var Dimensions=require('Dimensions')




export default class Register extends Component<{}> {

    // 构造
    constructor(props) {
        super(props);
        var totalCount=10;
        // 初始状态
        this.state = {
            count: totalCount
        };

        this.componentWillUnMount = this.componentWillUnMount.bind(this);
    }

    //加载完后
    componentDidMount() {
        this.count()
    }

    //控件卸载的时候
    componentWillUnMount() {
        clearInterval(this.timer);
    }

    count() {
        this.timer = setInterval(()=>this.setState({
                count: this.state.count - 1
            }
        ), 1000);

    }


    render() {
        if (this.state.count == 0) {
            clearInterval(this.timer);
        }
        return (
            
                {this.state.count}
            
        );
    }
}




你可能感兴趣的:(RN中的定时器)