微信小程序展示倒计时

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


}
相关推荐
m0_587383004 小时前
无人自助拼豆系统的全栈开发方案
java·后端·struts·小程序·架构
AI源码4 小时前
雷山苗韵 - 非物质文化遗产传承平台
小程序·非遗小程序
CRMEB系统商城4 小时前
CRMEB开源商城系统(PHP)v8.0,打造全新AI二开商城底座!
开发语言·人工智能·小程序·开源·php
一木 之林4 小时前
微信小程序开发全攻略·文章版|01 入门与环境搭建
人工智能·学习·计算机视觉·微信小程序·notepad++
StevenLdh5 小时前
第八期 · 情绪闯关挑战盒:一个「无惩罚」成就系统的数据建模
java·微信小程序
眨眼猫会务智能体6 小时前
眨眼猫会务智能体会务系统的服务响应怎么样?出问题多久有人接?
安全·小程序·会务
微订校园外卖平台6 小时前
校园跑腿小程序上线:四端架构与校园外卖系统履约链路拆解
小程序·架构·外卖系统·本地生活·微订
拖孩7 小时前
AI 时代,个人开发者到底能不能靠一个小程序赚到钱
前端·后端·微信小程序
m0_5873830019 小时前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析