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 ...
随机推荐
- MVC四大筛选器—ExceptionFilter
该筛选器是在系统出现异常时触发,可以对抛出的异常进行处理.所有的ExceptionFilter筛选器都是实现自IExceptionFilter接口 public interface IExceptio ...
- C#进阶のMEF注入
1.什么是MEF 先来看msdn上面的解释:MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库. 应用程序开发人员可利用该库发现并使用扩展, ...
- [SDOi2012]吊灯
嘟嘟嘟 这题想了半天,搞出了一个\(O(10 * d * n)\)(\(d\)为\(n\)的约数个数)的贪心算法,就是能在子树内匹配就在子树内匹配,否则把没匹配的都交给父亲,看父亲能否匹配.交上去开了 ...
- C# WebApi 获取客户端ip地址
转自:http://www.cnblogs.com/weixing/p/5674078.html References required: HttpContextWrapper - System.We ...
- 1-tomcat简介
一.tomcate的目录结构说明: 1.bin:存放服务器启动和关闭的命令文件.2.conf:存放服务器的配置信息文件3.lib:存放服务器自身需要的所有jar文件,也称为全局jar文件(只要部署在当 ...
- 微信硬件平台(一) 公众号 ESP8266 Arduino LED
微信硬件平台 本文目的,使用微信公众号控制ESP8266的LED开和关.进一步使用微信当遥控器(避免写APP或者IOS或者小程序),控制一切设备.给两个关键的总教程参考. 官网教程 微信硬件平台 微 ...
- WiFi-ESP8266入门http(3-1)网页认证上网-post请求(原教程)
教程:http://geek-workshop.com/thread-37484-1-1.html 源码:链接:https://pan.baidu.com/s/1yuYYqsM-WSOb0AbyAT0 ...
- jenkins使用4----git maven工具连接
搭建完git服务器 将jenkins服务器的的公钥传到git服务器的/home/git/.ssh的authorized_keys文件下 ssh端口2994 创建工程 配置完maven发现创建项目没有m ...
- HotSpot虚拟机对象探秘(对象创建,对象内存布局,对象访问定位)
以常用的HotSpot虚拟机和JAVA内存区域堆为例,探讨对象的创建,对象的内存布局以及对象的访问定位 一.对象的创建 1)类加载:虚拟机遇到一条new指令时,先检测这个指令的参数能否在常量池中定位到 ...
- 1-STM32带你入坑系列(STM32介绍)
由于自己的物联网开发板上的单片机是用的STM32,但是有些朋友没有用过,所以我将用这块开发板,带着大家入门STM32 先介绍一下STM32,我是在大三下学期的时候开始接触STM32,当时是想做一个小车 ...