当前位置: 首页 > news >正文

uniapp 实现时分秒 分别倒计时

效果

<view class="issue-price-countdown">
         <CountDown :endTimestamp="1745996085000"></CountDown>
 </view>

引入组件

import CountDown from '@/components/CountDown.vue';

<template>
    <view class="countdown">
        <view class="time-box hour">{{ currentHours }}</view>
        <text class="colon">:</text>
        <view class="time-box minute">{{ currentMinutes }}</view>
        <text class="colon">:</text>
        <view class="time-box second">{{ currentSeconds }}</view>
    </view>
</template>

<script>
    export default {
        props: {
            // 接收13位时间戳(结束时间)
            endTimestamp: {
                type: Number,
                required: true
            }
        },
        data() {
            return {
                remainingTime: 0,  // 剩余总秒数
                timer: null,
                currentHours: 0,
                currentMinutes: 0,
                currentSeconds: 0
            }
        },
        methods: {
          // 初始化倒计时
            initCountdown() {
                this.clearTimer()
                this.calculateRemaining()
                
                if (this.remainingTime > 0) {
                    this.timer = setInterval(() => {
                        this.updateTime()
                    }, 1000)
                }
            },
            // 计算剩余时间
            calculateRemaining() {
                const now = Date.now()
                this.remainingTime = Math.max(0, Math.floor((this.endTimestamp - now) / 1000))
                this.updateDisplayTime()
            },
            
            // 更新时间显示
            updateDisplayTime() {
                let seconds = this.remainingTime % 60
                let minutes = Math.floor(this.remainingTime / 60) % 60
                const hours = Math.floor(this.remainingTime / 3600)
    
                // 实现级联更新效果
                if (this.currentSeconds === 0 && seconds === 59) {
                    this.currentMinutes = minutes
                }
                if (this.currentMinutes === 0 && minutes === 59) {
                    this.currentHours = hours
                }
    
                this.currentSeconds = this.pad(seconds)
                this.currentMinutes = this.pad(minutes)
                this.currentHours = this.pad(hours)
            },
            // 每秒更新
            updateTime() {
                this.remainingTime = Math.max(0, this.remainingTime - 1)
                this.updateDisplayTime()
    
                if (this.remainingTime <= 0) {
                    this.clearTimer()
                    this.$emit('timeup')
                }
            },
            
            // 补零函数
            pad(n) {
                return n < 10 ? '0' + n : n
            },
            
            // 清除定时器
            clearTimer() {
                if (this.timer) {
                    clearInterval(this.timer)
                    this.timer = null
                }
            }
        },
        watch: {
            endTimestamp: {
                immediate: true,
                handler(newVal) {
                    this.initCountdown()
                }
            }
        },
        created() {
          
        }
    }
</script>

<style lang="scss" scoped>
    .countdown {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .time-box {
            width: 52rpx;
            height: 52rpx;
            background: #313131;
            line-height: 52rpx;
            text-align: center;
            border-radius: 10rpx 10rpx 10rpx 10rpx;
            font-weight: bold;
        }
        .colon {
            color: #313131;
            margin: 0 12rpx;
        }
    }
</style>

相关文章:

  • 从零开始学Python游戏编程48-二维数组2
  • git did not exit cleanly (exit code 128) 已解决
  • 【uniapp】在UniApp中检测手机是否安装了某个应用
  • Canvas基础篇:图形绘制
  • 卫星变轨轨迹和推力模拟(单一引力源)MATLAB
  • AI驱动的决策智能系统(AIDP)和自然语言交互式分析
  • 金融风控的“天眼”:遥感技术的创新应用
  • SAP MM 定价程序步骤及细节
  • 第二章-科学计算库NumPy
  • 华为云汪维敏:AI赋能应用现代化,加速金融生产力跃升
  • vs2019编译occ7.9.0时,出现fatal error C1060: compiler is out of heap space
  • Mysql查询异常【Truncated incorrect INTEGER value】
  • vscode详细配置Go语言相关插件
  • win11 终端 安装ffmpeg 使用终端Scoop
  • OpenCV实战教程 第一部分:基础入门
  • Java List分页工具
  • 零部件设计行业如何在数字化转型中抓住机遇?
  • LangChain 核心模块:Data Conneciton - Vector Stores
  • 基于LangChain构建最小智能体(Agent)实现指南
  • 26个脑影像工具包合集分享:从预处理到SCI成图
  • 马上评|启动最高层级医政调查,维护医学一方净土
  • 新华时评:防范安全事故须臾不可放松
  • 国家医保局副局长颜清辉调任人社部副部长
  • 解密62个“千亿县”:强者恒强,新兴产业助新晋县崛起
  • 普京与卢卡申科举行会晤,将扩大在飞机制造等领域合作
  • 2024“好评中国”网络评论大赛结果揭晓