当前位置:首页 > VUE

vue实现按钮计时

2026-02-19 07:10:17VUE

Vue 实现按钮计时功能

在 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>

使用组件封装

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

vue实现按钮计时

<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实现jqueryui

vue实现jqueryui

Vue 实现 jQuery UI 功能 在 Vue 项目中实现类似 jQuery UI 的功能,可以通过原生 Vue 组件或第三方库来实现。以下是几种常见 jQuery UI 功能的 Vue 替代方案…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue实现增删

vue实现增删

Vue 实现增删功能 在 Vue 中实现增删功能通常涉及数据绑定、事件处理和列表渲染。以下是基于 Vue 2 或 Vue 3 的实现方法: 数据定义 初始化一个数组用于存储列表数据,并在 Vue 实…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…