当前位置:首页 > uni-app

uniapp倒计时组件

2026-02-05 16:57:26uni-app

uniapp倒计时组件实现方法

使用内置组件实现

uniapp本身没有专门的倒计时组件,但可以通过<text><view>结合定时器实现基础功能。

uniapp倒计时组件

<template>
  <view>
    <text>{{countdown}}</text>
  </view>
</template>

<script>
export default {
  data() {
    return {
      countdown: '00:00:00',
      timer: null,
      totalSeconds: 3600 // 1小时倒计时
    }
  },
  mounted() {
    this.startCountdown()
  },
  methods: {
    startCountdown() {
      this.timer = setInterval(() => {
        if(this.totalSeconds <= 0) {
          clearInterval(this.timer)
          return
        }
        this.totalSeconds--
        this.formatTime()
      }, 1000)
    },
    formatTime() {
      const hours = Math.floor(this.totalSeconds / 3600)
      const minutes = Math.floor((this.totalSeconds % 3600) / 60)
      const seconds = this.totalSeconds % 60
      this.countdown = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

使用第三方插件

uni-ui提供了uni-countdown组件,可以直接使用:

uniapp倒计时组件

<template>
  <view>
    <uni-countdown 
      :show-day="false" 
      :hour="1" 
      :minute="0" 
      :second="0"
      @timeup="timeup">
    </uni-countdown>
  </view>
</template>

<script>
import uniCountdown from '@/components/uni-countdown/uni-countdown.vue'
export default {
  components: {uniCountdown},
  methods: {
    timeup() {
      uni.showToast({
        title: '倒计时结束',
        icon: 'none'
      })
    }
  }
}
</script>

自定义高级倒计时组件

创建更灵活的倒计时组件,支持多种格式和回调:

<template>
  <view class="countdown-container">
    <text v-if="showDay">{{days}}天</text>
    <text>{{hours}}</text>
    <text>:</text>
    <text>{{minutes}}</text>
    <text>:</text>
    <text>{{seconds}}</text>
  </view>
</template>

<script>
export default {
  props: {
    endTime: {
      type: [String, Number],
      required: true
    },
    showDay: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      days: '00',
      hours: '00',
      minutes: '00',
      seconds: '00',
      timer: null
    }
  },
  mounted() {
    this.startCountdown()
  },
  methods: {
    startCountdown() {
      this.timer = setInterval(() => {
        const now = new Date().getTime()
        const end = new Date(this.endTime).getTime()
        const distance = end - now

        if (distance < 0) {
          clearInterval(this.timer)
          this.$emit('timeup')
          return
        }

        this.days = Math.floor(distance / (1000 * 60 * 60 * 24)).toString().padStart(2, '0')
        this.hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)).toString().padStart(2, '0')
        this.minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)).toString().padStart(2, '0')
        this.seconds = Math.floor((distance % (1000 * 60)) / 1000).toString().padStart(2, '0')
      }, 1000)
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style>
.countdown-container {
  display: flex;
  align-items: center;
}
</style>

注意事项

定时器需要在组件销毁时清除,避免内存泄漏。倒计时精度受系统性能影响,长时间运行的倒计时建议定期与服务器时间同步。对于需要极高精度的场景,可以使用WebSocket实时同步时间。

移动端页面进入后台时定时器可能被暂停,重新进入前台时需要检查时间差并更新倒计时状态。跨平台开发时注意不同平台的定时器行为差异。

标签: 倒计时组件
分享给朋友:

相关文章

uniapp组件写法

uniapp组件写法

Uniapp 组件的基本写法 Uniapp 的组件写法与 Vue 类似,支持单文件组件(.vue 文件)。一个基本的组件通常包含三个部分:<template>、<script>…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue轮播组件实现

vue轮播组件实现

vue轮播组件实现方法 使用第三方库(如Swiper) 安装Swiper库: npm install swiper 在Vue组件中引入: <template> <div cl…

vue 实现组件刷新

vue 实现组件刷新

组件局部刷新 在Vue中实现组件刷新可以通过强制重新渲染组件来实现。常用的方法有以下几种: 使用v-if指令 通过切换v-if条件触发组件的销毁和重建 <template> <…

vue实现日历组件

vue实现日历组件

Vue 日历组件实现步骤 基础结构搭建 使用Vue 3的Composition API或Options API创建组件框架。核心结构包括年份/月份切换按钮、星期标题栏和日期网格。 <templ…

vue 实现动态组件

vue 实现动态组件

Vue 动态组件的实现方法 在 Vue 中,可以通过 <component> 标签结合 is 属性实现动态组件切换。以下是具体实现方式: 使用 is 属性绑定组件名 <templ…