React Native实现验证码倒计时功能

前端之家收集整理的这篇文章主要介绍了React Native实现验证码倒计时功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

React Native实现验证码倒计时功能
实现倒计时功能使用的是核心方法 setInterval,setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭

  1. startCountDown() {
  2. this.interval = setInterval(() => { this.setState({ countdown: this.getCountdown() - 1 }); },1000); }

当 App 切换至后台之后,setInterval 将不会执行,而当 App 从后台切换至前台时,setInterval 将继续执行,所以对于切换至后台的情况需要监听 App 状态,记录变化的时间。React Native 提供了 AppState 监听 App 状态改变,AppState 一共有三种状态,包括以下三种。

  • active - 应用正在前台运行
  • background - 应用正在后台运行。用户既可能在别的应用中,也可能在桌面
  • inactive - 这是一个过渡状态,不会在正常的 React Native 应用中出现
  1. componentDidMount() {
  2. AppState.addEventListener('change',this._handleAppStateChange);
  3. }
  4.  
  5. componentWillUnmount() {
  6. AppState.removeEventListener('change',this._handleAppStateChange);
  7. this.interval && clearInterval(this.interval);
  8. }
  9.  
  10. _handleAppStateChange = (nextAppState) => {
  11. if (this.state.appState === 'active' && nextAppState.match(/inactive|background/)) {
  12. this.backgroundTime = new Date().getTime() / 1000;
  13. }
  14. if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
  15. this.backgroundTime = utils.fomatFloat(new Date().getTime() / 1000 - this.backgroundTime,0);
  16. }
  17. this.setState({appState: nextAppState});
  18. }

最后完整代码

  1. import React,{
  2. PureComponent,} from 'react';
  3. import {
  4. TouchableOpacity,Text,AppState
  5. } from 'react-native';
  6.  
  7. import * as utils from '../utils';
  8.  
  9. class CountDown extends PureComponent {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. appState: AppState.currentState,countdown: -1,disabled: false
  14. };
  15. this.backgroundTime = 0;
  16. }
  17.  
  18. componentDidMount() {
  19. AppState.addEventListener('change',this._handleAppStateChange);
  20. }
  21.  
  22. componentWillUnmount() {
  23. AppState.removeEventListener('change',this._handleAppStateChange);
  24. this.interval && clearInterval(this.interval);
  25. }
  26.  
  27. _handleAppStateChange = (nextAppState) => {
  28. if (this.state.appState === 'active' && nextAppState.match(/inactive|background/)) {
  29. this.backgroundTime = new Date().getTime() / 1000;
  30. }
  31. if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
  32. this.backgroundTime = utils.fomatFloat(new Date().getTime() / 1000 - this.backgroundTime,0);
  33. }
  34. this.setState({appState: nextAppState});
  35. }
  36.  
  37. setCountdown(countdown) {
  38. this.setState({
  39. countdown: countdown
  40. });
  41. }
  42.  
  43. getCountdown() {
  44. return this.state.countdown;
  45. }
  46.  
  47. startCountDown() {
  48. this.interval = setInterval(() => { if (this.backgroundTime < this.getCountdown()) { this.setState({ countdown: this.getCountdown() - this.backgroundTime - 1 },()=>{ this.backgroundTime = 0; if (this.getCountdown() < 0) { this.interval && clearInterval(this.interval); } if (this.getCountdown() >= 0) { this.setState({ disabled: true }); } else { this.setState({ disabled: false }); } }); } else { this.setCountdown(-1); this.setState({ disabled: false }); this.interval && clearInterval(this.interval); } },1000); this.setState({ disabled: true }); } render() { const { onPress,style,vcodeTextStyle } = this.props; return ( <TouchableOpacity disabled={this.state.disabled} onPress={onPress} style={style}> {this.state.countdown >= 0 ? <Text style={vcodeTextStyle}> {`${this.state.countdown}`}秒 </Text> : <Text style={vcodeTextStyle}> 获取验证码 </Text> } </TouchableOpacity> ); } } export default CountDown;

其中 utils.fomatFloat 是截取小数点后几位的一个方法

  1. //保留小数点后几位
  2. export function fomatFloat(src,pos) {
  3. return Math.round(src * Math.pow(10,pos)) / Math.pow(10,pos);
  4. }

猜你在找的React相关文章