uniapp-秒杀倒计时,精简版。

需求:购买商品后,以每天18.30为结束时间,对其进行倒计时。

思路:拿后端返回的下单时间(时间戳),到指定时间进行倒计时。

第一步:我先封装了一个处理时间戳的方法。

getData(n){
	let now = new Date(n),
	y = now.getFullYear(),
	m = now.getMonth() + 1,
	d = now.getDate();
	return y + "/" + (m < 10 ? "0" + m : m) + "/" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
	},

用getData处理时间戳,可以得到具体时间:

这样就可以拿到具体某天的具体时间了。

第二步:再写一个方法,用来处理倒计时

computedTime(t){
                //定义orderTime,也就是下单时的时间戳,乘1000得出的是毫秒数
				let orderTime = t * 1000;
				//定义AOTime,得出下单当天的结束时间
				let AOtime = this.getData(orderTime);
                //定义endTime,拿到当天截至时间的时间戳,我这里设置的是每天下午18.30结束
				let endTime = new Date(new Date(new Date(AOtime).toLocaleDateString()).getTime() + 18.5 * 60 * 60 * 1000) .getTime();
                //定义nowTime,拿到当前时间的时间戳
				let nowTime = new Date().valueOf();
                //这时就可以作比较,如果下单当天的结束时间小于当前的时间,就可以直接return掉。
				if (endTime < nowTime) {
					return '已失效'
				};
                //如果下单时间小于当前的时间,并且当前的时间小于下单当天的结束时间,就可以对其进行处理
				if (orderTime < nowTime && nowTime < endTime) {
                    //定义diffValue :当天结束时间的时间戳,减去现在的时间戳,得到差值
					const diffValue = endTime - nowTime;
                    //时
					const hours = parseInt(diffValue / 1000 / 60 / 60 % 24);
                    //分
					const min = parseInt(diffValue / 1000 / 60 % 60);
                    //秒
					const sec = parseInt(diffValue / 1000 % 60);
					return (hours<10 ?'0'+hours:hours) + '时' + (min<10?'0'+min:min ) + '分' + (sec<10?'0'+sec:sec ) + '秒'
				}
			},

 第三步,处理后端返回过来的数据,我这里自己在data里定义模拟

TimeList:[
					{times:1659859200},//8.7-16.00
					{times:1659920400},//8.8-9.00
					{times:1660007995} //8.9-12.00
				]
cDown() {
				this.TimeList = this.TimeList.map(v=>({
					...v,
					timer : this.computedTime(v.times)
				}))
				setInterval(()=>{
						this.TimeList = this.TimeList.map(v=>({
						...v,
						timer : this.computedTime(v.times)
					}))
				},1000)
			},

 然后再页面加载的时候,调用这个方法,就可以得到倒计时了

mounted(){
			this.cDown();
		},

效果如图:

 最后附上完整代码:

<template>
	<view>
		<view class="">倒计时</view>
		<view class="" v-for="Item,index in TimeList" :key="index">距离秒杀还剩:{{Item.timer}}</view>
	</view>
</template>

<script>
	export default {
		data(){
			return{
				//模仿后端返回的下单时间
				TimeList:[
					{times:1659859200},//8.7-16.00
					{times:1659920400},//8.8-9.00
					{times:1660007995} //8.9-12.00
				]
			}
		},
		mounted(){
			this.cDown();
		},
		methods: {
			cDown() {
				this.TimeList = this.TimeList.map(v=>({
					...v,
					timer : this.computedTime(v.times)
				}))
				setInterval(()=>{
						this.TimeList = this.TimeList.map(v=>({
						...v,
						timer : this.computedTime(v.times)
					}))
				},1000)
			},
			getData(n){
				let now = new Date(n),
				y = now.getFullYear(),
				m = now.getMonth() + 1,
				d = now.getDate();
				return y + "/" + (m < 10 ? "0" + m : m) + "/" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0, 8);
			},
			computedTime(t){
				let orderTime = t * 1000;
				let AOtime = this.getData(orderTime);
				let endTime = new Date(new Date(new Date(AOtime).toLocaleDateString()).getTime() + 18.5 * 60 * 60 * 1000) .getTime();
				let nowTime = new Date().valueOf();
				if (endTime < nowTime) {
					return '已失效'
				};
				if (orderTime < nowTime && nowTime < endTime) {
					const diffValue = endTime - nowTime;
					const hours = parseInt(diffValue / 1000 / 60 / 60 % 24);
					const min = parseInt(diffValue / 1000 / 60 % 60);
					const sec = parseInt(diffValue / 1000 % 60);
					return (hours<10 ?'0'+hours:hours) + '时' + (min<10?'0'+min:min ) + '分' + (sec<10?'0'+sec:sec ) + '秒'
				}
			},
		},
		
	}
</script>
<style scoped lang="scss">
	
</style>

前端小白,大神勿喷,感谢🙏🙏🙏

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值