vue页面使用多个定时器

问题描述

vue页面使用多个定时器

html:

<div class="prod-item">
      <ul>
        <li
          v-for="(item, index) in state.list"
          :key="index"
          :class="[
            item.isDisabled ? 'active_li_02' : 'active_li_01',
            'flex ml-10 mr-10  mt-25',
          ]"
        >
          <div class="img">
            <img :src="item.imageUrl" alt="" />
          </div>
          <div class="item-right">
            <div
              :class="[
                item.isDisabled ? 'active_title_02' : 'active_title_01',
                'title',
              ]"
            >
              {{ item.name }}
            </div>
            <van-button
              type="default"
              :class="[
                item.isDisabled ? 'active_btn_02' : 'active_btn_01',
                'btn mt-30',
              ]"
              @click.stop="checkLoginUser(item)"
              :disabled="item.isDisabled"
            >
              {{ item.saleTit }}
            </van-button>
          </div>
        </li>
      </ul>
    </div>

js:

js:请求数据,遍历数组,然后根据数据字段判断,如果服务器的开始时间小于服务器的系统时间那就倒计时,appointmentStatus 这个字段为2的时候 且服务器的开始时间小于服务器的系统时间.
let appointStart = new Date( item.appointStart.replace(/-/g, "/") ).getTime(); 这个是兼容ios的时间格式

  const getProdList = async () => {
      //预售商品列表
      await $api.fns.PreSale.getPreSaleList({
        params: {
          iconType: 2,
        },
      })
        .then((res) => {
          if (res?.data) {
            console.log(res.data);
            // `appointment_status`'预约状态 1已上架、2已下架',
            state.list = res.data;
            res.data.map((item) => {
              item.isDisabled = true;
              item.saleTit = "等待预约";
              item.timer = null;
              if (item.appointStart) {
                let appointStart = new Date(
                  item.appointStart.replace(/-/g, "/")
                ).getTime();
                let systemDate = new Date(
                  item.systemDate.replace(/-/g, "/")
                ).getTime();
                item.times = Math.round(
                  parseInt(appointStart - systemDate) / 1000
                );
              }
            });
            state.list = res.data;
            state.list.map((item, index) => {
              if (item.appointmentStatus === 2) {
                if (item.times) {
                  // 还没有开始预购
                  if (item.times > 0) {
                    startCountdown(item.times, index);
                    // 预购结束
                  } else {
                    item.isDisabled = true;
                    item.saleTit = "预购结束";
                  }
                  // 时间为空的时候,就只有预购结束,和立即预购两种状态
                } else {
                  item.isDisabled = true;
                  item.saleTit = "预购结束";
                }
              } else {
                item.isDisabled = false;
                item.saleTit = "立即预购";
              }
            });
           
          }
        })
        .catch((error) => {
          console.log(error);
        });
    };

因为每一个定时器的时间都不一样,所以每一个定时器结束了要清除定时器 window.clearInterval(state.list[index].timer);

  // 倒计时
    const startCountdown = (times, index) => {
      console.log("index", index, state.list);
      // 跟开始时间相比如果当前时间小于开始时间,那就还没有开始
      // let times = Math.round(parseInt(appointStart - systemDate) / 1000);
      if (times > 0) {
        state.list[index].timer = setInterval(() => {
          state.list[index].times--;
          console.log("state.times-----111", state.list[index].times);
          if (state.list[index].times === 0) {
            state.list[index].times = 0;
            state.list.map(() => {
              state.list[index].isDisabled = false;
              state.list[index].saleTit = "立即预购";
            });

            window.clearInterval(state.list[index].timer);
          }
        }, 1000);
      }
    };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值