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 ...
随机推荐
- echarts修改上下左右的边距
grid: { top: '4%', left: '3%', right: '4%', ...
- mumu安卓模拟器使用教程
安装教程: 1http://mumu.163.com/ 在这网址里面下载 2.安装 我的是mac 安装时需要输入你的你电脑密码 但是也报错,你点击旁边的提示就会告诉去安全与隐私里点击允许就好了 很 ...
- JAVA序列化和反序列化XML
package com.lss.utils; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.Bu ...
- [MySQL性能优化系列] 聚合索引
1. 普通青年的索引使用方式 假设我们有一个用户表 tb_user,内容如下: name age sex jack 22 男 rose 21 女 tom 20 男 ... ... ... 执行SQL语 ...
- vue的过滤器语发及应用案例
1.使用地方: 双花括号插值处或 组件属性处 例: {{ message | capitalize }} <div v-bind:id="rawId | formatId&quo ...
- [找工作] 2019秋招|从春招到秋招,Java岗经验总结(收获AT)
转自(有更多) https://blog.csdn.net/zj15527620802/article/month/2018/10 前言 找工作是一件辛酸而又难忘的历程.经历过焦虑.等待.希望,我们最 ...
- [TJOI2018]碱基序列
嘟嘟嘟 现在看到字符串就想到SAM,所以很担心kmp啥的会不会忘了-- 这题感觉挺暴力的:首先当然要把\(s\)建成SAM,然后令\(dp[i][j]\)表示到第\(i\)组时,SAM上节点\(j\) ...
- 001学习Python的ABC模块(转)
http://yansu.org/2013/06/09/learn-Python-abc-module.html 1.abc模块作用 Python本身不提供抽象类和接口机制,要想实现抽象类,可以借助a ...
- tomcat 改端口 运维最最重要的就是有看日志的习惯
tomcat一台机器上多实例更改端口需要改三个端口 改tomcat关闭端口 <Server port="9006" shutdown="SHUTDOWN" ...
- Python(x,y) 的 FTP 下载地址
因为 Python(x,y) 软件包托管在 Google code 上 https://code.google.com/p/pythonxy/,所以国内比较难下载. 这里推荐一个 FTP 下载地址:f ...