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

相关文章

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 时间字符串转时间

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)