Uniapp实现多个订单批量待付款倒计时

Uniapp实现多个订单批量待付款倒计时

思路

当客户不付款生成一个待付款的订单这个待付款的订单需要获取它30分钟后的时间然后保存到数据库

然后前台将当前时间减去从数据库拿到的时间进行倒计时

注意 必须要24小时制的时间

首先准备一个times.js

/**
 * 订单未付款  倒计时 
 * @param {*} time  结束时间
 */
function countDownFun(time) {
	
    let startTime = new Date().getTime();
	console.log("startTime:"+startTime)
    // let startTime = new Date(); //当前时间
	
    let end = new Date(time).getTime(); //结束时间
	console.log("end:"+end)
    // console.log(end)
    let result = parseInt((end - startTime) / 1000); //计算出豪秒
    let d = parseInt(result / (24 * 60 * 60)); //用总共的秒数除以1天的秒数
    let h = parseInt((result / (60 * 60)) % 24); //精确小时,用去余
    let m = parseInt((result / 60) % 60); //剩余分钟就是用1小时等于60分钟进行趣余
    let s = parseInt(result % 60);
    //统一格式的显示
    d < 10 ? d = '0' + d : d;
    h < 10 ? h = '0' + h : h;
    m < 10 ? m = '0' + m : m;
    s < 10 ? s = '0' + s : s;
    //当倒计时结束时,改变内容
    if (result <= 0) {
        return "订单超时";
    }
    if (d == '00') {
        return h + "时" + m + "分" + s + "秒";;
    }
    if (d == '00' && h == '00') {
        return m + "分" + s + "秒";;
    }
    if (d == '00' && h == '00' && m == '00') {
        return s + "秒";;
    }
    return d + "天" + h + "时" + m + "分" + s + "秒";
}
 
module.exports = {
    countDownFun
 
}

方法(超时就重新获取一遍订单 times==你从后台获取的订单数据) toBePaid: []

	timer(times) {
				let that = this;
				this.myInterv = setInterval(() => {
					//reserveNo
					times.forEach((item, index) => {
						if (item.currentTime == '订单超时') {
							let url = "http://localhost:9977/reserve/UpdatePaYstate"
							uni.request({
								url: url,
								method: "post",
								header: {
									'content-type': 'application/x-www-form-urlencoded'
								},
								data: {
									reserveNo: item.reserveNo,
								},
								success: (res) => {
                                    //再查询一遍数据
									that.getToBePaid(that.optionsid)
								}
							})
						} else { //关键点   item.expiretime==你每次生成一个待付款的订单要把当前时间存储进数据库的时间
                            //必须要是24小时制不然会有问题
							item.expiretime;
							that.$set(item, 'currentTime', countDownFun(item.expiretime));
						}

					});
				}, 1000);


			},

后端

         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
           long currentTime = System.currentTimeMillis();
                currentTime += 30 * 60 * 1000;
                Date dates = new Date(currentTime);
                String format = dateFormat.format(dates);
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现多个批量倒计时,可以考虑使用定时器和循环遍历的方式来处理。 具体实现步骤如下: 1. 在页面中定义一个数组,用于存储需要倒计时的数据。 2. 在页面的 onShow 生命周期函数中,遍历数组,为每个数据项设置一个定时器,并将定时器的 id 存储到对应的数据项中。 3. 定时器的回调函数中,更新对应数据项的倒计时时间,并判断倒计时是否结束,如果结束,则清除定时器。 4. 当页面 onHide 时,清除所有定时器,防止定时器持续运行,浪费资源。 以下是示例代码: ```html <template> <view> <view v-for="(item, index) in list" :key="index"> <text>{{ item.name }}</text> <text>{{ item.remainingTime }}</text> </view> </view> </template> <script> export default { data() { return { list: [ { name: 'item1', remainingTime: 60, timerId: null }, { name: 'item2', remainingTime: 120, timerId: null }, { name: 'item3', remainingTime: 180, timerId: null } ] } }, onShow() { this.list.forEach((item, index) => { const timerId = setInterval(() => { item.remainingTime-- if (item.remainingTime <= 0) { clearInterval(item.timerId) item.timerId = null } }, 1000) item.timerId = timerId }) }, onHide() { this.list.forEach((item, index) => { if (item.timerId) { clearInterval(item.timerId) item.timerId = null } }) } } </script> ``` 在上述代码中,list 数组中存储了三个数据项,每个数据项包含了需要倒计时的时间和对应的定时器 id。在页面的 onShow 生命周期函数中,遍历 list 数组,为每个数据项设置一个定时器,并将定时器 id 存储到对应的数据项中。定时器的回调函数中,更新对应数据项的倒计时时间,并判断倒计时是否结束,如果结束,则清除定时器。当页面 onHide 时,清除所有定时器,防止定时器持续运行,浪费资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值