当前位置: 首页 > 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>

http://www.xdnf.cn/news/227899.html

相关文章:

  • 大数据平台与数据仓库的核心差异是什么?
  • MySQL RR (Repeatable Read) 隔离级别规则细节
  • 【计算机视觉】目标检测:深度解析Detectron2:Meta开源目标检测与图像分割框架实战指南
  • Linux Nginx网站服务【完整版】
  • 从高端制造到民生场景:天元轻量化软件的“破局”之路
  • 【QT】编写第一个 QT 程序 对象树 Qt 编程事项 内存泄露问题
  • 大语言模型 06 - 从0开始训练GPT 0.25B参数量 - MiniMind 实机配置 GPT训练基本流程概念
  • ASP.NET MVC​ 入门与提高指南六
  • 一套SaaS ERP管理系统源码,支持项目二开商用,SpringBoot+Vue+ElementUI+UniAPP
  • 11.Spring Boot 3.1.5 中使用 SpringDoc OpenAPI(替代 Swagger)生成 API 文档
  • 若依Vue + Spring Boot:前后端参数传递实践与 @RequestParam、@RequestBody、@ModelAttribute 使用详解
  • 解决vscode cmake提示检测到 #include 错误
  • 【Hive入门】Hive高级特性:事务表与ACID特性详解
  • Ubuntu 24.04 终端美化
  • 第一章 OpenCV篇-配置及基础知识-Python
  • 常用开发脚本工具推荐
  • java网络原理5
  • 【C++】数据结构 九种排序算法的实现
  • python对接马来西亚股票完整代码
  • 微前端框架选型指南
  • Go并发控制模式:基于Channel的实践与优化
  • Github 2025-04-30 C开源项目日报 Top10
  • 3dgs渲染 Nvdiffrast
  • getattr 的作用
  • 6.应用层
  • 搭建PCDN大节点,服务器该怎么配
  • Vue3 后台管理系统模板
  • redis 数据类型新手练习系列——List类型
  • 驱动开发系列54 - Linux Graphics QXL显卡驱动代码分析(一)设备初始化
  • 微信小程序 XSS 防护知识整理