当前位置:首页 > VUE

vue实现倒计时抢券

2026-03-06 21:23:46VUE

实现倒计时抢券功能

在Vue中实现倒计时抢券功能需要结合计时器、状态管理和UI交互。以下是具体实现方法:

核心逻辑实现

使用setInterval创建计时器,在组件销毁时清除避免内存泄漏

data() {
  return {
    countdown: 60, // 倒计时秒数
    timer: null,
    isDisabled: true // 按钮禁用状态
  }
},
methods: {
  startCountdown() {
    this.timer = setInterval(() => {
      if(this.countdown <= 0) {
        clearInterval(this.timer)
        this.isDisabled = false
        return
      }
      this.countdown--
    }, 1000)
  }
},
mounted() {
  this.startCountdown()
},
beforeDestroy() {
  clearInterval(this.timer)
}

界面显示处理

使用计算属性格式化显示时间

vue实现倒计时抢券

computed: {
  formattedTime() {
    const minutes = Math.floor(this.countdown / 60)
    const seconds = this.countdown % 60
    return `${minutes}:${seconds < 10 ? '0' + seconds : seconds}`
  }
}

模板部分代码

<template>
  <div class="coupon-container">
    <div class="countdown">剩余时间: {{ formattedTime }}</div>
    <button 
      :disabled="isDisabled"
      @click="handleGrabCoupon"
      :class="{ 'disabled-btn': isDisabled }"
    >
      {{ isDisabled ? '等待抢券' : '立即抢券' }}
    </button>
  </div>
</template>

样式优化

添加视觉反馈提升用户体验

vue实现倒计时抢券

.disabled-btn {
  background-color: #ccc;
  cursor: not-allowed;
}
.coupon-container {
  text-align: center;
  padding: 20px;
}
.countdown {
  font-size: 18px;
  color: #f56c6c;
  margin-bottom: 15px;
}

抢券逻辑实现

点击事件处理函数

methods: {
  handleGrabCoupon() {
    if(this.isDisabled) return

    // 调用API接口
    api.grabCoupon().then(res => {
      this.$message.success('抢券成功')
    }).catch(err => {
      this.$message.error(err.message)
    })
  }
}

服务器时间同步

解决客户端时间不准问题

async syncServerTime() {
  const { serverTimestamp } = await api.getServerTime()
  const clientTime = Date.now()
  this.timeDiff = serverTimestamp - clientTime
}

完整组件示例

export default {
  data() {
    return {
      countdown: 60,
      timer: null,
      isDisabled: true,
      timeDiff: 0
    }
  },
  computed: {
    formattedTime() {
      const minutes = Math.floor(this.countdown / 60)
      const seconds = this.countdown % 60
      return `${minutes}:${seconds < 10 ? '0' + seconds : seconds}`
    }
  },
  methods: {
    startCountdown() {
      this.timer = setInterval(() => {
        if(this.countdown <= 0) {
          clearInterval(this.timer)
          this.isDisabled = false
          return
        }
        this.countdown--
      }, 1000)
    },
    async handleGrabCoupon() {
      if(this.isDisabled) return

      try {
        const res = await api.grabCoupon()
        this.$message.success('抢券成功')
      } catch (err) {
        this.$message.error(err.message)
      }
    },
    async syncServerTime() {
      const { serverTimestamp } = await api.getServerTime()
      this.timeDiff = serverTimestamp - Date.now()
    }
  },
  async mounted() {
    await this.syncServerTime()
    this.startCountdown()
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}

以上实现包含了倒计时核心逻辑、界面显示、样式优化和服务器时间同步等完整功能,可根据实际需求进行调整。

标签: 倒计时vue
分享给朋友:

相关文章

vue实现tab标签

vue实现tab标签

Vue 实现 Tab 标签的方法 使用动态组件和 v-for 渲染标签 通过 v-for 循环生成标签页标题,结合 v-bind:class 动态切换激活状态。使用 v-show 或动态组件 <…

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue 实现grid

vue 实现grid

在Vue中实现Grid布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid布局 CSS Grid是一种强大的布局系统,可以直接在Vue组件的样式中使用。以下是一个简单的示例:…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…