Taro关于多个数组同时根据时间展示倒计时的代码组件

2024-04-01 12:28

本文主要是介绍Taro关于多个数组同时根据时间展示倒计时的代码组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我们通常在做秒杀活动时,会有活动开始或者活动结束倒计时
而在活动列表中,需要做统一处理
以下为做的关于倒计时的组件~

primaryColor可忽略,是关于倒计时时间的主题色
startTime活动开始时间
endTime活动结束时间
refresh方法为其中一个倒计时结束后,页面的重新渲染方法,经常是重新请求列表接口,方法自定义

组件文件

import React, { Component, CSSProperties } from 'react';
import { View } from '@tarojs/components';
import { isFunction } from 'lodash-es';
import { hexToRgba, iosTimeStringFomat } from '@/application/utils/format';
import './index.scss';interface PaymentRewardCountdownProps {primaryColor: string;startTime: string;endTime: string;refresh(): void;
}
interface PaymentRewardCountdownState {timeRemaining: TimeRemaining | null;
}
interface TimeRemaining {type: 'start' | 'end' | 'ended';value: number;
}export default class PaymentRewardCountdown extends Component<PaymentRewardCountdownProps,PaymentRewardCountdownState
> {constructor(props: PaymentRewardCountdownProps) {super(props);this.state = {timeRemaining: null};}componentDidMount(): void {const { startTime, endTime, refresh } = this.props;const iosStartTime = iosTimeStringFomat(startTime);const iosEndTime = iosTimeStringFomat(endTime);this.timer = setInterval(() => {const now = new Date();const start = new Date(iosStartTime);const end = new Date(iosTimeStringFomat(iosEndTime));if (now < start) {const diff = start.getTime() - now.getTime();this.setState({ timeRemaining: { type: 'start', value: diff } });} else if (now < end) {const diff = end.getTime() - now.getTime();this.setState({ timeRemaining: { type: 'end', value: diff } });} else {this.setState({ timeRemaining: { type: 'ended', value: 0 } });clearInterval(this.timer);if (isFunction(refresh)) {refresh();}}}, 1000);}componentWillUnmount() {if (this.timer) {clearInterval(this.timer);}}private timer: NodeJS.Timeout;private renderCountdown = () => {const { primaryColor } = this.props;const { timeRemaining } = this.state;if (!timeRemaining) {return null;}const numberStyle: CSSProperties = {color: primaryColor,background: `${hexToRgba(primaryColor, 0.5)}`};const timeInfo = formatSeconds(timeRemaining.value / 1000);return (<View className={classes.right}><View className={classes.prefix}>{timeRemaining.type === 'start' ? '距离开始' : '距离结束:'}</View><View className={classes.number} style={numberStyle}>{timeInfo.days}</View><View className={classes.symbol}></View><View className={classes.number} style={numberStyle}>{timeInfo.hours}</View><View className={classes.symbol}></View><View className={classes.number} style={numberStyle}>{timeInfo.minutes}</View><View className={classes.symbol}></View><View className={classes.number} style={numberStyle}>{timeInfo.seconds}</View><View className={classes.symbol}></View></View>);};render() {return <View className={prefix}>{this.renderCountdown()}</View>;}
}const prefix = 'payment-reward-countdown';
const classes = {right: `${prefix}__right`,prefix: `${prefix}__right__prefix`,number: `${prefix}__right__number`,symbol: `${prefix}__right__symbol`
};
interface DataInfo {days: number;hours: string;minutes: string;seconds: string;
}
function addLeadingZero(num: number): string {if (num < 10) {return `0${num}`;}return num.toString();
}
function formatSeconds(time: number): DataInfo {const days = Math.floor(time / (3600 * 24));const hours = addLeadingZero(Math.floor((time % (3600 * 24)) / 3600));const minutes = addLeadingZero(Math.floor((time % 3600) / 60));const seconds = addLeadingZero(Math.floor(time % 60));return {days,hours,minutes,seconds};
}

样式文件

.payment-reward-countdown {display: flex;flex-direction: row;align-items: center;justify-content: space-between;&__right {height: 32px;display: flex;flex-direction: row;align-items: center;justify-content: flex-end;&__prefix {font-size: $font-size-mini;}&__number {padding: 0 6px;height: 32px;font-weight: 500;font-size: 22px;color: #FF3B30;line-height: 32px;text-align: center;background: rgba(255,59,48,0.05);border-radius: 4px;}&__symbol {padding: 0 6px;color: #666666;line-height: 28px;text-align: center;font-size: 20px;font-weight: bold;}}
}

在数组的item文件中,直接调用时间组件

<PaymentRewardCountdownstartTime={market.startTime}endTime={market.endTime}refresh={this.refresh}primaryColor={primaryColor}/>

示例图
在这里插入图片描述

这篇关于Taro关于多个数组同时根据时间展示倒计时的代码组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/867160

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库

MySQL多实例管理如何在一台主机上运行多个mysql

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间