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

界面显示处理

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

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>

样式优化

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

.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)
    })
  }
}

服务器时间同步

解决客户端时间不准问题

vue实现倒计时抢券

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实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现mouseout

vue实现mouseout

Vue 实现 mouseout 事件的方法 在 Vue 中实现 mouseout 事件可以通过多种方式完成,以下是几种常见的方法: 使用 v-on 指令 Vue 提供了 v-on 指令(或简写为 @…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…