• 计时器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>
            )
        }
    }
    

      

  • 相关阅读:
    指定的参数已超出有效值的范围。参数名:sit ,先仔细看看错误和我的一样不一样
    简单说下C#变量的作用域
    C#常用的字符串处理方法
    驼峰命名、帕斯卡命名、匈牙利命名--三种命名方法
    Python的安装
    Python下numpy的使用
    命名法:骆驼(Camel)、帕斯卡(pascal)、匈牙利(Hungarian)、下划线(_)
    PHP中高级面试问题集锦
    利用python,生成word
    python实现网页截图
  • 原文地址:https://www.cnblogs.com/xiaojun-zxj/p/6367095.html
Copyright © 2020-2023  润新知