微信小程序展示倒计时

html

html 复制代码
  <view class="countdown">  
    <text>倒计时:</text>  
    <text wx:for="{{countdown}}" wx:key="index">{{item}}</text>  
  </view>

ts

javascript 复制代码
 data: {
    countdown: [], // 存放倒计时数组  
    targetTime: '', // 目标时间戳  
    intervalId: null, // 定时器ID 
}
startCountdown () {  
  const that = this;  
  // 每秒更新一次倒计时  
  this.data.intervalId = setInterval(function () {  
    const now = new Date().getTime();  
    const diff = that.data.targetTime - now;  
      
    if (diff <= 0) {  
      // 倒计时结束,清除定时器  
      clearInterval(that.data.intervalId);  
      that.setData({  
        countdown: ['倒计时结束'],  
      });  
    } else {  
      // 计算剩余的天、时、分、秒  
      const days = Math.floor(diff / (1000 * 60 * 60 * 24));  
      const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));  
      const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));  
      const seconds = Math.floor((diff % (1000 * 60)) / 1000);  
        
      that.setData({  
        countdown: [days, '天', hours, '时', minutes, '分', seconds, '秒'],  
      });  
    }  
  }, 1000);     
  },
  onLoad: function (options) {   
  this.setData({  
    targetTime: new Date('2024-04-28 16:53:10').getTime(),  
  });  
  this.startCountdown();  


}
相关推荐
forestsea6 小时前
Python进阶编程总结
开发语言·python·notepad++
peachSoda711 小时前
uniapp小程序生成海报/图片并保存分享
小程序·uni-app
说私域12 小时前
社交电商引流策略中的让利行为及其影响——基于开源AI智能名片、链动2+1模式与S2B2C商城小程序的分析
人工智能·小程序·开源·产品运营·零售
罗政13 小时前
AI小程序+SpringAI+管理后台+源码+支持动态添加大模型+支持动态添加AI应用
人工智能·小程序
社会底层无业大学生14 小时前
微信小程序跳2
微信小程序·小程序·notepad++
泉城老铁16 小时前
springboot对接微信小程序,发送订阅消息
微信小程序
说私域17 小时前
开源AI大模型AI智能名片S2B2C商城小程序:科技浪潮下的商业新引擎
人工智能·小程序·开源·产品运营·零售
zru_960218 小时前
微信小程序事件详解
微信小程序·小程序
依辰19 小时前
小程序自动化构建与版本管理方案优化
前端·javascript·微信小程序