当前位置:首页 > VUE

vue实现按钮计时

2026-02-19 07:10:17VUE

Vue 实现按钮计时功能

在 Vue 中实现按钮计时功能通常涉及倒计时逻辑和按钮状态的动态切换。以下是两种常见实现方式:

基础倒计时实现

<template>
  <button @click="startCountdown" :disabled="isCounting">
    {{ buttonText }}
  </button>
</template>

<script>
export default {
  data() {
    return {
      count: 60,
      isCounting: false,
      timer: null
    }
  },
  computed: {
    buttonText() {
      return this.isCounting ? `剩余${this.count}秒` : '获取验证码'
    }
  },
  methods: {
    startCountdown() {
      this.isCounting = true
      this.timer = setInterval(() => {
        this.count--
        if (this.count <= 0) {
          clearInterval(this.timer)
          this.isCounting = false
          this.count = 60
        }
      }, 1000)
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

使用组件封装

创建可复用的倒计时按钮组件:

<template>
  <button 
    :class="{ 'disabled': counting }"
    @click="triggerCountdown"
    :disabled="counting"
  >
    {{ counting ? `${count}秒后重试` : text }}
  </button>
</template>

<script>
export default {
  props: {
    duration: {
      type: Number,
      default: 60
    },
    text: {
      type: String,
      default: '发送验证码'
    }
  },
  data() {
    return {
      count: this.duration,
      counting: false,
      timer: null
    }
  },
  methods: {
    triggerCountdown() {
      this.counting = true
      this.$emit('click')

      this.timer = setInterval(() => {
        this.count--
        if (this.count <= 0) {
          this.resetCountdown()
        }
      }, 1000)
    },
    resetCountdown() {
      clearInterval(this.timer)
      this.counting = false
      this.count = this.duration
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style scoped>
.disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>

使用 Composition API 实现

Vue 3 的 Composition API 版本:

<template>
  <button @click="start" :disabled="isActive">
    {{ isActive ? `${seconds}秒后重试` : '点击发送' }}
  </button>
</template>

<script setup>
import { ref } from 'vue'

const seconds = ref(60)
const isActive = ref(false)
let timer = null

const start = () => {
  isActive.value = true
  timer = setInterval(() => {
    seconds.value--
    if (seconds.value <= 0) {
      clearInterval(timer)
      isActive.value = false
      seconds.value = 60
    }
  }, 1000)
}
</script>

注意事项

  • 清除定时器:务必在组件销毁时清除定时器,防止内存泄漏
  • 状态重置:倒计时结束后恢复初始状态
  • 禁用状态:通过 disabled 属性防止重复点击
  • 样式处理:可添加过渡效果提升用户体验

以上实现可根据具体需求调整倒计时时长、按钮文本和样式表现。

vue实现按钮计时

标签: 按钮vue
分享给朋友:

相关文章

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tab…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…

vue实现颜色

vue实现颜色

Vue 实现颜色的方法 在 Vue 中实现颜色管理可以通过多种方式,以下是一些常见的方法: 使用 CSS 变量动态绑定颜色 在 Vue 组件的样式中定义 CSS 变量,并通过 Vue 的数据绑定动…