• 计时器60s


    计时器是经常用到的功能,下面以react nativ的例子简介来写一个倒计时60s的小demo。

    代码如下:

    import React, { Component } from 'react';
    import {
        Text,
        View,
        TouchableOpacity
    } from 'react-native';
    import {
        Button,
        Content,
        Container,
        Header,
        InputGroup,
        Input,
        List,
        ListItem,
        Thumbnail,
        Title,
    } from 'native-base';
    
    import MyTheme from '../../themes/myTheme';
    import SetNewPasswordView from './../login-register/set-newPassword-view'
    
    export default class FindPasswordView extends Component {
        constructor(props) {
            super(props);
            this.state = {
                data: 60,
                sendButton: true
            };
            this._index = 60;
            this._timer = null;
        }
    
        countTime() {
            this.setState({sendButton: false});
    
            this._timer = setInterval(()=> {
                this.setState({data: this._index--});
                if (this.state.data <= 0) {
                    this._timer && clearInterval(this._timer);
                    this.setState({sendButton: true});
                }
            }, 1000);
        }
    
        componentWillUnmount() {
            this._timer && clearInterval(this._timer);
        }
    
        _navigate(name, component, role, type = 'Normal') {
            this.props.navigator.push({
                component: component,
                name: name,
                passProps: {
                    name: name,
                    role: role
                },
                onPress: this._onPress,
                rightText: '右边',
                type: type
            })
        }
    
        render() {
            return (
                <Container theme={MyTheme}>
                    <Header>
                        <Button transparent onPress={()=>this.props.navigator.pop()}>
                            <Text style={{color:'#35b873'}}>取消</Text>
                        </Button>
                        <Title>找回密码</Title>
                    </Header>
                    <Content style={{backgroundColor:'#eff2f3'}}>
                        <View
                            style={{flex:1,flexDirection:'row',backgroundColor:'#fff',marginTop:20,borderBottomWidth:0.5,borderColor:'#ccc'}}>
                            <Text style={{marginTop:13,marginLeft:15}}>手机号</Text>
                            <Input style={{marginLeft:20}} placeholderTextColor={'#ccc'} placeholder="请输入手机号..."/>
                        </View>
                        <ListItem
                            style={{backgroundColor:'#fff',paddingLeft:0,marginLeft:0,marginTop:20,borderBottomWidth:0}}>
                            <InputGroup>
                                <Input style={{marginLeft:5,marginRight:10}} placeholderTextColor={'#ccc'}
                                       placeholder="请输入验证码..."/>
                                {
                                    this.state.sendButton ? <Button style={{marginLeft:5,marginRight:10}} small success
                                                                    onPress={this.countTime.bind(this)}>发送验证码</Button> :
                                        <Button disabled style={{marginLeft:5,marginRight:10}}
                                                small>请等待{this.state.data}s</Button>
                                }
    
    
                            </InputGroup>
                        </ListItem>
                        <Button block success onPress={()=>this._navigate('找回密码',SetNewPasswordView)}
                                style={{marginLeft:15,marginRight:15,marginTop:40,padding:15,height:45}}>
                            验证
                        </Button>
                    </Content>
                </Container>
            )
        }
    }
    

      

  • 相关阅读:
    合肥程序员欢迎进QQ群49313181同城程序员职业交流群
    新一代程序员的思考
    ThinkPHP开发系列一框架搭建
    ASP.NET MVC4+EF系列之五 架构介绍
    ASP.NET MVC4+EF系列之阶段源码一
    gcc g++ Linux下动态库_静态库 指定路径问题
    [转]accept() 产生的Socekt端口是多少?
    阿里云计算资深总监唐洪:飞天大规模分布式计算系统解析
    [转] C++中##(两个井号)和#(一个井号)用法
    deep learning 深度学习
  • 原文地址:https://www.cnblogs.com/xiaojun-zxj/p/6367095.html
Copyright © 2020-2023  润新知