小程序倒计时

针对一个页面列表中多个倒计时的难题

解决思路:
计时器单放一个数组,因为要实时操作,避免列表数据过大操作太笨重。
根据计时器列表的index 与列表数据的 index 相对,去找到自己对应的倒计时器。
页面刷新数据时要先 清除所有定时器 ,再重新加载,不然会出现连跳或者秒数来回跳动的问题。


// data里声明  
timerList: [],
allTimerList: []

// js 传入秒数 与 index
timerhandle(timeSeconds,index){
    if (timeSeconds){
      this.setData({
        ['timerList[' + index + ']']: [parseInt(timeSeconds/60/60%24), parseInt(timeSeconds/60%60), parseInt(timeSeconds%60)]
      },()=>{
        let timer = setInterval(() => {
          timeSeconds--
          // 倒计时到0,可以加入自定义函数处理
          if (timeSeconds <= 0) {
            clearInterval(timer)
          }
          this.setData({
            ['timerList[' + index + ']']: [parseInt(timeSeconds / 60 / 60 % 24), parseInt(timeSeconds / 60 % 60), parseInt(timeSeconds % 60)]
          })
        }, 1000)
        let ll = this.data.allTimerList
        ll.push(timer)
        this.setData({ allTimerList: ll })
      })
    }else{
      this.setData({
        ['timerList[' + index + ']']: ['00','00','00']
      })
    }
    
  },
  // 清除定时器
  clearAllTimer() {
  	 this.data.allTimerList.forEach((el, index) => {
        clearInterval(el)
      })
      this.setData({ allTimerList: [], timerList: [] },()=>{
		
      })
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值