ReactNative短信验证码倒计时控件的实现代码

由于最近刚开始认真的搞RN,可能有一些封装的不是最佳实践,还是希望大家多提意见,和大家一起进步吧。本文介绍了ReactNative短信验证码倒计时控件,分享给大家

功能

根据项目的需要,需要写一个自定义的控件,实现如下功能:

  1. 默认文字为点击获取验证码
  2. 点击后出现60秒的倒计时
  3. 颜色,字号可调
  4. 倒计时过程中,再次点击需要忽略掉
  5. 倒计时完成后文本恢复成点击获取验证码
  6. 再几次点击同之前

其实说了这么多,就是个最普通的验证码的功能。。。

效果

效果图如下:(录的图片比较一般,对付着看吧)

实现原理

自己封装了个控件,它内部含有一个Text控件,然后我们又写了一个timer,然后负责倒计时,然后每次都需要判断一下是否继续,然后加了一个flag字段,判断是否接受下次点击事件,当倒计时结束之后还需要将初始状态重置回去即可。

代码

控件代码

import axios from 'axios';

class MyCountTime extends Component {
constructor(props) {
super(props);
let timeLeft = this.props.timeLeft > 0 ? this.props.timeLeft : 5;
let width = this.props.width || 100;
let height = this.props.height || 50;
let color = this.props.color || '#42A5F5';
let fontSize = this.props.fontSize || 30;
let fontWeight = this.props.fontWeight || '600';
let borderColor = this.props.borderColor || '#42A5F5';
let borderWidth = this.props.borderWidth || 1;
let borderRadius = this.props.borderRadius || 4;
let backgroundColor = this.props.backgroundColor || '#42A5F5';
let begin = 0;
let press = this.props.press;

this.afterEnd = this.props.afterEnd || this._afterEnd;
this.style = this.props.style;

this.state = {
  timeLeft: timeLeft,begin: begin
};
this.countTextStyle = {
  textAlign: 'center',color: '#42A5F5',fontSize: fontSize,fontWeight: fontWeight

};
this.countViewStyle = {
  backgroundColor: backgroundColor,alignItems: 'center',borderColor: borderColor,borderWidth: borderWidth,borderRadius: borderRadius,width: width,height: height
}

}

countdownfn(timeLeft,callback,begin) {
if (timeLeft > 0) {
this.state.begin = 1;
console.log("===lin===>");

  let that = this;
  let interval = setInterval(function () {
    if (that.state.timeLeft < 1) {
      clearInterval(interval);
      callback(that)
    } else {
      let totalTime = that.state.timeLeft;
      that.setState({
        timeLeft: totalTime - 1
      })
    }
  },1000)
}

}

_beginCountDown() {
if (this.state.begin === 1){
return;
}
let time = this.state.timeLeft;
console.log("===lin===> time " + time);
let afterEnd = this.afterEnd;
let begin = this.state.begin;
console.log("===lin===> start " + begin);
this.countdownfn(time,afterEnd,begin)
}

_afterEnd(that) {
console.log('------------time over');
that.setState({
begin : 0,timeLeft : 5,})
}

componentDidMount() {

}

render() {
return (
<View style={{position:'absolute',top:13,right:43,height:30}}>
<Text
onPress={this._beginCountDown.bind(this)}
style={{color: '#42A5F5',fontSize: 17,height:40,zIndex:999}}> { this.state.begin === 0 ? '点击获取验证码' : this.state.timeLeft}

  </View>
)

}
}

应用代码

当然这只是,最简单的应用的代码,我们还提供了很多的自定义的属性,大家可以根据自己项目的需要,去调节这些参数。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: 首先在编辑器里面插入javascript代码: 确定后会在编辑器插入这样的代码: <pre
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换为SyntaxHighlighter代码高亮插件 上一篇“让kindeditor显示高亮代码”中已经
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高
JS对象如何转为json格式字符串
JS怎么获取图片原始宽高
怎么在click事件中调用多个js函数
js如何往数组中添加新元素
js如何拆分字符串
JS怎么对数组内元素进行求和
JS如何判断屏幕大小
js怎么解析json数据
js如何实时获取浏览器窗口大小
原生JS实现别踩白块小游戏(五)
原生JS实现别踩白块小游戏(一)