vue如何给循环出来的列表分别做倒计时展示

一、代码如下

<templete>
    <div class="auctionnowlist" v-for="(item) in nowspec" :key="item.specId">
        <div class="auctionnowlistleft">
          <img :src="item.hfileIds" class="auctionnowlistimg" />
        </div>
        <div class="auctionnowlistcen">
          <p class="cennamezc">{{item.specName}}</p>
          <p class="centime">
            <span>距开始结拍</span>
            <span style="margin-left:5px;color:#A43534;font-weight:450">{{timeChange(item.countDown)}}</span>
          </p>
          <p class="cenjp" :class="[item.status=='拍卖中'?'red':'']">{{item.status}}</p>
        </div>
        <div class="auctionnowlistright">
          <button class="gonumzc" @click="gotospec(item)">参与竞拍</button>
        </div>
    </div>
</templete>
<script>
     export default({
          data(){
              return{
                  nowspec:[],
                  timer:null,
              }
          },
          created(){ 
            if (this.timer) { //这一段是防止进入页面出去后再进来计时器重复启动
                clearInterval(this.timer); 
            }
         },
         mounted(){
            this.getlist();
            this.beginTimer();
         },
         methods:{
              getlist(){
                   //获取返回的列表数据
	             this.$axios({
			        method: "get",
			        url:baseUrl+"/test",//这里是请求接口
			      }).then(res => {
			          this.nowspec = res.data.data;
			          this.nowspec===null?this.nowspec = []:this.nowspec = this.nowspec;
			      })
              },
              beginTimer() { //这个计时器是每秒减去数组中指定字段的时间
			      this.timer = setInterval(() => {
			        for (let i = 0, len = this.nowspec.length; i < len; i++) {
			          const item = this.nowspec[i];
			          if (item.countDown > 0) {
			            item.countDown = item.countDown - 1;//我这里后台返回的是秒数,如果是日期需要自己转换一下
			          }
			        }
			      }, 1000);
		    },
		    timeChange(countDown) {
		       var $that = this;
		        if (countDown < 0) {
		            clearInterval(setInterval(this.timeChange(countDown), 1000)); //当无剩余时间了,就停止执行timeChange函数
		        }
        //求天时分秒 leftDate是毫秒
		        let leftSecond = $that.fillNumber(countDown % 60);
		        let leftMinute = $that.fillNumber((countDown / 60) % 60);
		        let leftHours = $that.fillNumber((countDown / (60 * 60)) % 24);
		        let leftDay = $that.fillNumber(countDown / (60 * 60) / 24);
		        return leftDay+" 天 "+leftHours+" 小时 "+leftMinute+" 分 "+leftSecond+" 秒 ";
		    },
		    fillNumber(num) {
		        let number = Math.floor(num); //向下取整
		        return number > 9 ? number : "0" + number;
		    },
         }
     })
</script>

二、效果如下
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swagLi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值