vue 实现多个列表倒计时

 1.计算现在距离到期还有多长时间方法

 

 function countDownFun(time) {
        let nowTime = new Date().getTime(); //现在时间
        if (nowTime <= time) {
            //获取时间差
            let timediff = Math.round((time - nowTime) / 1000);
            console.log(timediff);
            //获取还剩多少天
            let day = parseInt(timediff / 3600 / 24);
            //获取还剩多少小时
            let hour = parseInt((timediff / 3600) % 24);
            //获取还剩多少分钟
            let minute = parseInt((timediff / 60) % 60);
            //获取还剩多少秒
            let second = timediff % 60;
            return day + "天" + hour + "小时" + minute + "分" + second + "秒";
        } else {
            return "00天00小时00分00秒";
        }
    }

2.执行倒计时方法

    实现方式是遍历后端数据,根据到期时间out_time 然后调用方法countdownFun 得出剩余时间,(time = time-1000*60*60*12 为计算到第二天12点的时间)

  find2(){
                var _this = this ;
                _this.dataList.forEach(function (date) {
                    //将字符串转换为date类型
                  var time = new Date(date.out_time).getTime();
                  time = time - 1000*60*60*12;
                  //计算剩余时间
                  var outTime = countDownFun(time);
                  //给集合添加outTime;
                  Vue.set(date,'outTime',outTime);
              })
            },

3.展示数据

 

 4.效果展示

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果您使用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”。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值