小程序,列表多个定时器的实现

列表多个定时器的实现

本篇是taro框架下的小程序实现

  1. 思想:每一个定时器相当于一个timeindex的对象,将多个这样的对象存入timeList数组,使用延时定时器执行方法functionAfunctionA内循环timeList,让每个time减一,并判断是否需要循环,同时循环完之后更新UI;

  2. 实现

// 数据
timeoutFlag: any = 0
timeoutList: any = []
hasBreak = 0
// 生命周期处理
componentDidHide() {
    this.timeoutFlag && clearTimeout(this.timeoutFlag);
}

componentWillUnmount() {
    this.timeoutFlag && clearTimeout(this.timeoutFlag);
}
//装载
this.timeoutList = []
this.timeoutFlag && clearTimeout(this.timeoutFlag);

this.timeoutList.push({ // 把未支付剩余时间加入倒计时队列
  index: index,
  time: item.expireMills,
  isEnd: false
});

if (this.timeoutList.length) {
  this.countDownNow(); // 运行倒计时
}
//倒计时结束
changeCancelStatus(index){
    if (index >= 0 && index < this.state.orderList.length) {
        this.state.orderList[index].dealState = 100
        this.state.orderList[index].dealStateMsg = '已取消'
    }
}

// 倒计时
countDownNow() {
  clearTimeout(this.timeoutFlag);
  this.timeoutFlag = setTimeout(() => {
    for (let i = 0; i < this.timeoutList.length; i++) {
      this.timeoutList[i].time -= 1000;
      if (this.timeoutList[i].isEnd) {
          break
      }
      if (this.timeoutList[i].time < 0) {
        this.hasBreak++
        this.changeCancelStatus(this.timeoutList[i].index)//
        this.timeoutList[i].isEnd = true
        this.forceUpdate()
      } else {
          this.formatTime(this.timeoutList[i]);
      }
    }

    if (this.hasBreak === this.timeoutList.length) {
      
    } else {
      this.countDownNow();
    }
  }, 1000);
  this.forceUpdate()
}

// 倒计时赋值
formatTime (timeObj) {
    this.state.leftTimeList[timeObj.index] = formatTimeString({
      time: timeObj.time
    });
}
// 时间格式化
function formatTimeString(op) {
  let nowOp = {
    ...op
  };

  let milliseconds = nowOp.time - (parseInt(nowOp.time / 1000, 10) * 1000);

  nowOp.time = (nowOp.time - milliseconds) / 1000; // 转为秒

  let seconds = nowOp.time - parseInt(nowOp.time / 60, 10) * 60;

  nowOp.time = (nowOp.time - seconds) / 60; // 转为分钟

  let minutes = nowOp.time - parseInt(nowOp.time / 60, 10) * 60;

  nowOp.time = (nowOp.time - minutes) / 60; // 转为小时

  let hour = nowOp.time - parseInt(nowOp.time / 24, 10) * 24;

  let day = (nowOp.time - hour) / 24;

  if (nowOp.completed) {
    let millisecondsLen = String(milliseconds).length;
    if (milliseconds < 3) {
      for (let i = 0; i < millisecondsLen; i++){
        milliseconds = '0' + milliseconds;
      }
    }
    seconds < 10 ? (seconds = '0' + seconds) : '';
    minutes < 10 ? (minutes = '0' + minutes) : '';
    hour < 10 ? (hour = '0' + hour) : '';
  }

  return {
    milliseconds,
    seconds,
    minutes,
    hour,
    day
  };
}

最后将timeList里面的时间展示在UI上面就可以了

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值