Vue——双(多)循环内设置多个倒计时

6 篇文章 0 订阅

记录一下

methods: {
	timer() {
		this.groupList.forEach(item => { 
			this.temp = setInterval(() => {
				item.groupItemRes.forEach(items => {
						items.countDown = this.countDownFun(items.endTime)
					})
					this.$forceUpdate()
			}, 1000)
		})
	},
	countDownFun(time) {
	  time--;
	  let nowTime = new Date().getTime(); // 获取当前时间
	  if (nowTime <= time) {
	    	let timediff = Math.round((time - nowTime) / 1000); // 获取时间差
	  	let day = parseInt(timediff / 3600 / 24); // 获取剩余天数
	  	let hour = parseInt((timediff / 3600) % 24); // 获取剩余小时
	  	let minute = parseInt((timediff / 60) % 60); // 获取剩余分钟
	  	let second = timediff % 60; // 获取剩余秒数
			if (hour < 10) { hour = "0" + hour }
			if (minute < 10) { minute = "0" + minute }
			if (second < 10) { second = "0" + second }
	  	return day + "天" + hour + "小时" + minute + "分" + second + "秒";
	  } else {
	    	return "00天00小时00分00秒";
	  }
	},
},
destroyed() {
	//切记页面销毁需要销毁
	clearInterval(this.temp);
	console.log(this.temp, "销毁");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您使用Vue.js编写订单列表,您可以使用以下代码来实现多个待支付计时: 1. 首先,在组件的data选项中,创建一个名为“orders”的数组,其中包含您的订单对象。每个订单对象应该有一个名为“dueDate”的属性,表示订单的截止日期。 ``` data() { return { orders: [ { id: 1, dueDate: new Date('2021-09-30T23:59:59') }, { id: 2, dueDate: new Date('2021-10-05T23:59:59') }, { id: 3, dueDate: new Date('2021-10-10T23:59:59') }, ] } } ``` 2. 在组件的模板中,使用v-for指令循环渲染订单列表,并在每个订单中添加一个计时器组件。该计时器组件应该传递订单的截止日期作为参数。 ``` <template> <div> <h2>订单列表</h2> <ul> <li v-for="order in orders" :key="order.id"> 订单 #{{ order.id }} - 截止日期:{{ order.dueDate.toLocaleString() }} <countdown :due-date="order.dueDate"></countdown> </li> </ul> </div> </template> ``` 3. 创建一个名为“Countdown”的计时器组件,该组件接受一个名为“dueDate”的prop。在组件的data选项中,创建一个名为“timeLeft”的属性,表示剩余时间(秒)。 ``` <template> <div> 剩余时间:{{ timeLeft }} </div> </template> <script> export default { props: { dueDate: { type: Date, required: true } }, data() { return { timeLeft: 0 } }, } </script> ``` 4. 在组件的mounted钩子函数中,启动一个计时器,每秒更新一次“timeLeft”属性。计算剩余时间的方法使用Javascript的Date对象,通过计算当前时间和截止日期之间的差值来计算。 ``` <script> export default { props: { dueDate: { type: Date, required: true } }, data() { return { timeLeft: 0 } }, mounted() { setInterval(() => { const now = new Date() const diff = Math.floor((this.dueDate - now) / 1000) this.timeLeft = diff > 0 ? diff : 0 }, 1000) }, } </script> ``` 现在,您的订单列表应该具有多个待支付计时,每个订单都有一个独立的计时器。当订单的截止日期到达时,计时器将停止并显示“0”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值