vue 循环每个列表配置倒计时

//此处代码 复制即用 可能数据不同 替换下数据就行了
<template>
   <div class="all" ref="providall">
      <ul>
        <li v-for="(item,index) of iconlistall" :key="index">
           <div class="lileft">
              <p class="lilefttop">
                 <span class="spanleft">
                   已完成
                 </span>
                 <span>(提供帮助123456)</span>
              </p>
              <!-- <p>订单编号:123456</p>
              <p>预约时间:2018/10/10  14:31:56</p>
              <p>到期打款时间:</p> -->
              <p v-if="item.pre_at > 0">计时:{{time(item.pre_at)}}</p>
              <p v-else>计时: 已超时</p>
              <p>付款方式:支付宝</p>
            </div>
            <div class="liright">
               <p class="lirighttop">2000$</p>
               <p class="butp">查看截图</p>
               <p class="butp">详细资料</p>
            </div>
        </li>
      </ul>
   </div>
</template>

<script>
export default {
  data() {
    return {
      iconlist: [],
      ticker: null,
      d: "",
      h: "",
      m: "",
      s: ""
    };
  },
  computed:{ //这里是监听自定义数组的变化 因为有一个计时器每秒都会减去数组中字段的时间 所以 数组是每秒更新的
    iconlistall:{
      get(){
        return this.iconlist
      }
    }
  },
  methods: {
    beginTimer() { //这个计时器是每秒减去数组中指定字段的时间
      this.ticker = setInterval(() => {
        for (let i = 0, len = this.iconlist.length; i < len; i++) {
          const item = this.iconlist[i];
          if (item.pre_at > 0) {
            item.pre_at = item.pre_at - 1000;
          }
        }
      }, 1000);
    },
    time(time) { //这个函数是每秒把时间重新计算下
      if (time >= 0) {
        let d = Math.floor(time / 1000 / 60 / 60 / 24);
        let h = Math.floor((time / 1000 / 60 / 60) % 24);
        let m = Math.floor((time / 1000 / 60) % 60);
        let s = Math.floor((time / 1000) % 60);
         return '还有'+d+'天'+h+'时'+m+'分'+s+'秒';
      }
    },
  },
  created() {
  //这里是假的数据
    let list = [
      {
        pre_at: "2019-03-01"
      },
      {
        pre_at: "2019-03-01 16:18:36"
      },
      {
        pre_at: "2019-03-04"
      }
    ];
    //首先循环数组 在 正常的情况下这个数组是后台传递过来的 然后将这个数组中的最后截止时间减去当前时间获得时间戳
    for (let i = 0, len = list.length; i < len; i++) {
      const item = list[i];
      item.pre_at = new Date(item.pre_at).getTime() - new Date().getTime();
    }
    this.iconlist = list; //将改变后的数组赋值给自定义的数组 然后调用计时器 每秒减去指定字段的时间戳 -1000毫秒
    if (this.ticker) { //这一段是防止进入页面出去后再进来计时器重复启动
      clearInterval(this.ticker);
    }
    this.beginTimer(); //启动计时器减指定字段的时间
  },
  mounted() {
    //当页面加载时计算盒子的高度
    let html = document.getElementsByTagName("html")[0];
    let homeall = html.clientHeight - 44;
    this.$refs.providall.style.height = homeall + "px";
  },
  destroyed() {}
};
</script>

<style scoped lang='scss'>
.all {
  overflow-y: auto;
  display: flex;
  padding: 0rem 0.3rem;
  background: #ffffff;
  ul {
    width: 100%;
  }
  li {
    height: 2.1rem;
    padding: 0.3rem 0.3rem 0.3rem 0.3rem;
    display: flex;
    justify-content: space-between;
    margin-top: 0.2rem;
    background-color: #f5f5f5;
    border-radius: 0.1rem;
    .lileft {
      text-align: left;
      p {
        font-size: 0.24rem;
        color: #999999;
        margin-bottom: 0.11rem;
      }
      .lilefttop {
        font-size: 0.3rem;
        color: #999999;
        .spanleft {
          color: #333333;
        }
      }
    }
    .liright {
      text-align: right;
      .lirighttop {
        font-size: 0.3rem;
        color: #333333;
      }
      .butp {
        width: 1.4rem;
        height: 0.5rem;
        background-color: #999999;
        border-radius: 0.1rem;
        font-size: 0.24rem;
        color: #ffffff;
        line-height: 0.5rem;
        text-align: center;
        margin-top: 0.1rem;
      }
    }
  }
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值