微信小程序展示倒计时

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


}
相关推荐
CHU7290356 小时前
生鲜团购商城小程序:新鲜触手可及的便捷购物新体验
小程序
医疗信息化王工12 小时前
钉钉小程序开发实战:手术查询小程序
小程序·钉钉·手术查询
软件开发技术13 小时前
新版点微同城主题源码34.7+全套插件+小程序前后端 源文件
小程序·php
mon_star°1 天前
消防安全培训小程序项目亮点与功能清单
小程序
自然 醒1 天前
uni-app开发微信小程序,如何使用towxml去渲染md格式和html标签格式的内容?
微信小程序·uni-app·html
编程迪1 天前
基于Java和Vue开发的在线问诊系统医疗咨询小程序APP
小程序
CHU7290351 天前
知识触手可及:在线教学课堂APP的沉浸式学习体验
前端·学习·小程序
竟未曾年少轻狂1 天前
微信小程序-组件开发
微信小程序·小程序
CHU7290351 天前
在线教学课堂APP功能版块设计方案:重构学习场景的交互逻辑
java·学习·小程序·重构
焦糖玛奇朵婷1 天前
盲盒小程序开发,盲盒小程序怎么做
java·大数据·服务器·前端·小程序