微信小程序展示倒计时

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();  


}
相关推荐
mg66825 分钟前
微信小程序入门实例_____打造你的专属单词速记小程序
微信小程序·小程序
【ql君】qlexcel27 分钟前
Notepad++ 复制宏、编辑宏的方法
开发语言·javascript·notepad++··宏编辑·宏复制
程序员陆通33 分钟前
Vibe Coding开发微信小程序实战案例
微信小程序·小程序·notepad++·ai编程
「、皓子~1 小时前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
nbsaas-boot1 小时前
[特殊字符] 分享裂变新姿势:用 UniApp + Vue3 玩转小程序页面分享跳转!
小程序·uniapp·notepad++
老A技术联盟1 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
you45802 小时前
小程序学习笔记:使用 MobX 实现全局数据共享,实例创建、计算属性与 Actions 方法
笔记·学习·小程序
风铃喵游2 小时前
构建引擎: 打造小程序编译器
前端·小程序·架构
说私域2 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的抖音渠道力拓展与多渠道利润增长研究
人工智能·小程序·开源
MonkeyKing_sunyuhua3 小时前
微信小程序能不能获取物联网的上的设备数据
物联网·微信小程序·小程序