uni-app vue3 实现72小时倒计时功能

功能介绍 ,数组项有一个下单时间 ,比如今天下单在72小时内可以继续支付,超过则默认取消订单

  1. 页面按钮处 加上倒计时
html 复制代码
<!-- 倒计时 -->
<text v-if="item.timeLeft > 0">{{ formatTime(item.remaining) }}</text>
  1. 逻辑处理
javascript 复制代码
// 第一步 处理接口返回的数据 原本是没有倒计时 remaining这个值的 我们根据状态给数组项加上
console.log("列表数据", res);
res.forEach((item) => {
	if (item.actions.pay) {
		const createTime = new Date(item.create_time).getTime(); // 下单时间处理
		item.endTime = createTime + 72 * 3600 * 1000; // 计算72小时截止时间
		item.remaining = 0; // 剩余时间(毫秒)
		item.timeLeft = true; // 倒计时状态
	}
})
updateAllTimers();

// 第二步 统一定时器
let timer;

// 第三步 更新所有倒计时
const updateAllTimers = () => {
	const now = Date.now();
	listData.value.forEach(item => {
		const diff = item.endTime - now;
		item.remaining = diff;
		item.timeLeft = diff > 0;
	});
};

// 第四步 时间格式化函数
const formatTime = (milliseconds) => {
	if (milliseconds <= 0) return '00:00:00';
	const totalSeconds = Math.floor(milliseconds / 1000);
	const hours = Math.floor(totalSeconds / 3600).toString().padStart(2, '0');
	const minutes = Math.floor((totalSeconds % 3600) / 60).toString().padStart(2, '0');
	const seconds = (totalSeconds % 60).toString().padStart(2, '0');
	return `${hours}:${minutes}:${seconds}`;
};

// 生命周期管理
onMounted(() => {
	timer = setInterval(updateAllTimers, 1000);
});

// 卸载
onUnmounted(() => {
	clearInterval(timer);
});
相关推荐
害人终害己15 分钟前
‘vue-cli-service‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
前端·javascript·vue.js
excel12 小时前
研究 Vue 3 源码的收获
前端·vue.js
vx-程序开发20 小时前
【计算机毕设】51300童装超市管理系统的设计与实现
java·vue.js·spring boot·源码·课程设计·毕设·大作业
ym hyd 11120 小时前
题库建立与考试系统源码 Java+SpringBoot+Vue3 前后分离
java·开发语言·vue.js·spring boot·毕设
YZ1225521 天前
【Docker专题】使用Docker部署Vue-Flask项目前后端分离版【前端Docker部署】
前端·vue.js·docker
专业程序开发源1 天前
springboot古城景区管理系统88564-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·php·课程设计
小蒜学长1 天前
基于SSM+VUE的电影售票平台的设计与实现(代码+数据库+LW)
java·vue.js·spring boot·后端·ssm框架·电影售票平台
Q26433650231 天前
【有源码】基于Spring Boot+Vue的骑行爱好者平台的设计与实现-计算机毕设选题推荐
java·vue.js·spring boot·毕业设计·课程设计·数据可视化·源代码
FYKJ_20101 天前
SpringSecurity教室智能预约系统73972-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·spring·django
专业程序开发源1 天前
springboot酒店会员点餐系统22339-计算机课程设计、毕业设计
java·javascript·vue.js·spring boot·后端·php·课程设计