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);
});
相关推荐
一杯奶茶¥16 小时前
基于springboot的失物招领管理系统带万字文档 校园失物招领管理系统 失物认领管理系统java springboot vue
java·vue.js·spring boot·java项目
CHB20 小时前
HDC2026 演讲实录|AI 驱动的跨端进化:利用 uni-agent 快速构建高性能鸿蒙应用
uni-app·harmonyos
OpenTiny社区21 小时前
这次更新太良心!GenUI SDK v1.2.0 轻量化 + 稳流式 + 超强 Playground
前端·vue.js·ai编程
秃头网友小李21 小时前
前端难点:Element Plus 样式覆盖 —— :deep()、CSS 变量与滚动状态类名
前端·vue.js
英勇无比的消炎药1 天前
吃透 Sender 交互逻辑:提交快捷键事件与方法实战运用
vue.js
Agatha方艺璇1 天前
VUE复习笔记
前端·vue.js
2501_915918411 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
chushiyunen1 天前
vue el-pagination实现分页
javascript·vue.js·elementui
wanger611 天前
Vue学习笔记
前端·javascript·vue.js