React Native之倒计时组件的实现(ios android)
React Native之倒计时组件的实现(ios android)
一,需求分析
1,app需实现类似于淘宝的活动倒计时,并在倒计时结束时,活动也结束。
2,实现订单倒计时,并在倒计时结束时,订单关闭交易。
3,实现获取验证码倒计时。
二,技术实现
2.1,活动倒计时与订单倒计时的实现,源码如下:
componentDidMount() {
this.interval = setInterval(() => {
const date = this.getDateData(this.props.date);
if (date) {
this.setState(date);
} else {
this.stop();
this.props.onEnd();
}
}, 1000);
}
componentWillMount() {
const date = this.getDateData(this.props.date);
if (date) {
this.setState(date);
}
}
1,倒计时方法的实现:
getDateData(endDate) {
endDate = endDate.replace(/-/g, "/");
let diff = (Date.parse(new Date(endDate)) - Date.parse(new Date)) / 1000;
if (!!this.props.isOrederTime) {
diff = (Date.parse(new Date(endDate)) + (Number(this.props.isOrederTime) * 60 * 1000) - Date.parse(new Date)) / 1000;
}
if (diff <= 0) {
return false;
}
const timeLeft = {
years: 0,
days: 0,
hours: 0,
min: 0,
sec: 0,
millisec: 0,
};
if (diff >= (365.25 * 86400)) {
timeLeft.years = Math.floor(diff / (365.25 * 86400));
diff -= timeLeft.years * 365.25 * 86400;
}
if (diff >= 86400) {
timeLeft.days = Math.floor(diff / 86400);
diff -= timeLeft.days * 86400;
}
if (diff >= 3600) {
timeLeft.hours = Math.floor(diff / 3600);
diff -= timeLeft.hours * 3600;
}
if (diff >= 60) {
timeLeft.min = Math.floor(diff / 60);
diff -= timeLeft.min * 60;
}
timeLeft.sec = diff;
return timeLeft;
}
2,退出界面,清除定时器
componentWillUnmount() {
this.stop();
}
stop() {
clearInterval(this.interval);
}
3,数字不足时前面补0
// 数字前面补0
leadingZeros(num, length = null) {
let length_ = length;
let num_ = num;
if (length_ === null) {
length_ = 2;
}
num_ = String(num_);
while (num_.length < length_) {
num_ = '0' + num_;
}
return num_;
}
2.2,验证码倒计时与输入框
1,倒计时的实现(I18n 语言国际化),Api.sendVerifyCode调用后台发送验证码接口
//倒计时
daoJClick() {
//判断是否点击获取验证码
if(this.props.isClick){
this.props.isClick()
} if (this.props.mobile === '') {
Toast.show(I18n.t('Signin.Please_enter_phone_number'));
return;
}
if (!(/^1[345678]\d{9}$/.test(this.props.mobile))) {
Toast.show(I18n.t('Signin.pla_rightphoneNumber'));
return
}
if(this.state.isDisable){
return
}
Api.sendVerifyCode(this.props.mobile)
.then((data) => {
Toast.show(I18n.t('Signin.Verification_code_transmission_success')) }).catch((e) => {
});
this.setState({
isDisable: false,
});
const codeTime = this.state.timerCount -1;
const now = Date.now();
const overTimeStamp = now + codeTime * 1000 + 100;
/*过期时间戳(毫秒) +100 毫秒容错*/
this.interval = setInterval(() => {
/* 切换到后台不受影响*/
const nowStamp = Date.now();
if (nowStamp >= overTimeStamp) {
/* 倒计时结束*/
this.interval && clearInterval(this.interval);
this.setState({
timerCount: codeTime,
timerTitle: I18n.t('Signin.Get_verifying_code'),
isDisable: false,
});
} else {
const leftTime = parseInt((overTimeStamp - nowStamp) / 1000, 10);
this.setState({
timerCount: leftTime,
timerTitle: `(${leftTime})s`,
isDisable: true,
});
}
/* 切换到后台 timer 停止计时 */
}, 1000)
}
2,界面功能实现

<InputView
{...this.props}
returnKeyLabel={this.props.returnKeyLabel}
returnKeyType={this.props.returnKeyType}
align={this.props.align}
value={this.props.pin}
name={this.props.name}
hintText={this.props.hintText}
funcDisabled={this.state.isDisable}
onChangeText={this.props.onChangeText}
funcName={this.state.timerTitle}
funcOnPress={() => this.daoJClick()}/>
);
三,应用实例
3.1 活动与订单倒计时应用
import CountDown from "../CountDown";
<CountDown
date={endtime}
days={{ plural: '天 ', singular: '天 ' }}
onEnd={() => {
this.setState({
isEnd: true
})
}}
textColor={AppSetting.BLACK}
isHaveword={true}//是否有汉字
backgroundColor={'red'}
isOrederTime={AppSetting.OREDER_END_TIME}//是否是订单
textSize={AdaptationModel.setSpText(Platform.OS === 'ios' ? 18 : 20)}
/>
界面效果

3.2 验证码倒计时
import VeriCodeInput from "/VeriCodeInput"; <VeriCodeInput
style={styles.input}
inputStyle={{ color: 'white' }}
align={'center'}
value={this.state.captcha}
mobile={this.state.mobile}
backgroundColor={'transparent'}
funcNameStyle={{ color: AppSetting.GREEN }}
hintText={I18n.t('Signin.Please_enter_verification_code')}
isClick={() => { this.isinputverification() }}
onChangeText={(text) => this.setState({ captcha: text })} />
界面效果

React Native之倒计时组件的实现(ios android)的更多相关文章
- React Native之code-push的热更新(ios android)
React Native之code-push的热更新(ios android) React Native支持大家用React Native技术开发APP,并打包生成一个APP.在动态更新方面React ...
- React Native之支付集成(微信 支付宝)(ios android)
React Native之支付集成(微信 支付宝)(ios android) 一,需求分析 1.1,app在线充值与提现 二,技术介绍与集成 2.1,微信支付 2.1.1,Android配置 详细配置 ...
- [RN] React Native 封装选择弹出框(ios&android)
之前看到react-native-image-picker中自带了一个选择器,可以选择拍照还是图库,但我们的项目中有多处用到这个选择弹出框,所以就自己写了一下,最最重要的是ios和Android通用. ...
- React Native 简介:用 JavaScript 搭建 iOS 应用(2)
[编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...
- React Native 简介:用 JavaScript 搭建 iOS 应用 (1)
[编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...
- react native之组织组件
这些组件包括<TabView>,<NavigatorView>和<ListView>,他们实现了手机端最常用的交互和导航.你会发现这些组件在实际的项目中会非常有用. ...
- React Native 轻松集成分享功能(iOS 篇)
产品一直催我在 RN 项目中添加分享功能,一直没找到合适的库,今天让我看到了一个插件分享给大家. 在集成插件之前,需要在各大开放平台上成功注册应用,并通过审核(支持 3 个可选的主流平台).支持的平台 ...
- React Native 轻松集成统计功能(iOS 篇)
最近产品让我加上数据统计功能,刚好极光官方支持数据统计 支持了 React Native 版本 第一步 安装: 在你的项目路径下执行命令: npm install janalytics-react-n ...
- React Native常用第三方组件汇总--史上最全 之一
React Native 项目常用第三方组件汇总: react-native-animatable 动画 react-native-carousel 轮播 react-native-countdown ...
随机推荐
- python 基本数据类型--字符串实例详解
字符串(str) :把字符连成串. 在python中⽤', ", ''', """引起来的内容被称为字符串 . 注意:python中没有单一字符说法,统一称叫字 ...
- Python的datetime模块分析
datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.datetime ...
- Ribbon - WeightedResponseTimeRule原理
根据响应时间计算权重,响应越长,权重越低,权重越低的服务器,被选择的可能性就越低 //定时统计权重serverWeightTimer.schedule(new DynamicServerWeightT ...
- hive复杂类型实战
1.hive 数组简单实践: CREATE TABLE `emp`( `name` string, `emps` array<string>) ROW FORMAT SERDE 'org. ...
- 0、原生jdbc工厂类
一.代码结构 二.JDBCFactory.java package com.test; import java.io.IOException; import java.io.InputStream; ...
- CF617E XOR and Favorite Number
CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...
- Spring容器的简单实现(IOC原理)
引言:容器是什么?什么是容器?Spring容器又是啥东西?我给Spring容器一个对象名字,为啥能给我创建一个对象呢? 一.容器是装东西的,就像你家的水缸,你吃饭的碗等等. java中能作为容器的有很 ...
- 洛谷 P2802 回家
题目链接 https://www.luogu.org/problemnew/show/P2802 题目描述 小H在一个划分成了n*m个方格的长方形封锁线上. 每次他能向上下左右四个方向移动一格(当然小 ...
- Linux 下配置zookeeper集群
我们首先准备三台服务器,IP地址分别如下(前提是要先安装JDK) 192.168.100.101 192.168.100.102 192.168.100.103 1.配置主机名到IP地址的映射(此步骤 ...
- Oracle 取前几条记录
今天看了篇文章,对oracle取前几条数据的方式和说明,总结比较全,学习了,做个记录点.oracle 取前10条记录 以下内容是原始文章内容,用于做留存阅读. 1.oracle 取前10条记录 1) ...