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 Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

Java中Switch Case多个条件处理方法举例

《Java中SwitchCase多个条件处理方法举例》Java中switch语句用于根据变量值执行不同代码块,适用于多个条件的处理,:本文主要介绍Java中SwitchCase多个条件处理的相... 目录前言基本语法处理多个条件示例1:合并相同代码的多个case示例2:通过字符串合并多个case进阶用法使用

Java数组初始化的五种方式

《Java数组初始化的五种方式》数组是Java中最基础且常用的数据结构之一,其初始化方式多样且各具特点,本文详细讲解Java数组初始化的五种方式,分析其适用场景、优劣势对比及注意事项,帮助避免常见陷阱... 目录1. 静态初始化:简洁但固定代码示例核心特点适用场景注意事项2. 动态初始化:灵活但需手动管理代

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的