当前位置:首页 > VUE

vue实现按钮计时

2026-01-18 14:46:55VUE

实现按钮计时功能

在Vue中实现按钮计时功能可以通过以下步骤完成。这里提供一个基于Vue 2或Vue 3的实现方案,包含倒计时和禁用状态切换。

vue实现按钮计时

基本倒计时按钮实现

<template>
  <button 
    :disabled="isCounting" 
    @click="startCountdown"
  >
    {{ 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="{'countdown-button': true, 'disabled': isCounting}"
    :disabled="isCounting" 
    @click="handleClick"
  >
    {{ buttonText }}
  </button>
</template>

<script>
export default {
  props: {
    duration: {
      type: Number,
      default: 60
    }
  },
  data() {
    return {
      count: this.duration,
      isCounting: false,
      timer: null
    }
  },
  computed: {
    buttonText() {
      return this.isCounting ? `${this.count}秒后重试` : '获取验证码'
    }
  },
  methods: {
    handleClick() {
      this.$emit('click')
      this.startCountdown()
    },
    startCountdown() {
      this.isCounting = true
      this.timer = setInterval(() => {
        this.count--
        if (this.count <= 0) {
          this.resetCountdown()
        }
      }, 1000)
    },
    resetCountdown() {
      clearInterval(this.timer)
      this.isCounting = false
      this.count = this.duration
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
  }
}
</script>

<style scoped>
.countdown-button {
  padding: 8px 16px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.countdown-button:hover {
  background-color: #66b1ff;
}

.countdown-button.disabled {
  background-color: #c0c4cc;
  cursor: not-allowed;
}
</style>

使用Composition API实现(Vue3)

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

<script>
import { ref, computed, onBeforeUnmount } from 'vue'

export default {
  props: {
    duration: {
      type: Number,
      default: 60
    }
  },
  setup(props) {
    const count = ref(props.duration)
    const isCounting = ref(false)
    let timer = null

    const buttonText = computed(() => 
      isCounting.value ? `${count.value}秒后重试` : '获取验证码'
    )

    const startCountdown = () => {
      isCounting.value = true
      timer = setInterval(() => {
        count.value--
        if (count.value <= 0) {
          resetCountdown()
        }
      }, 1000)
    }

    const resetCountdown = () => {
      clearInterval(timer)
      isCounting.value = false
      count.value = props.duration
    }

    onBeforeUnmount(() => {
      clearInterval(timer)
    })

    return {
      count,
      isCounting,
      buttonText,
      startCountdown
    }
  }
}
</script>

功能扩展建议

  1. 添加发送验证码的API调用逻辑,在handleClick方法中先调用API,成功后再启动倒计时
  2. 添加加载状态,在API请求期间显示加载动画
  3. 使用本地存储保持倒计时状态,防止页面刷新后倒计时重置
  4. 添加声音或动画效果增强用户体验

以上实现可以根据实际项目需求进行调整,核心逻辑是通过setInterval实现倒计时功能,配合Vue的响应式系统更新UI状态。

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

相关文章

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue 实现树状

vue 实现树状

Vue 实现树状结构的方法 递归组件实现树状结构 递归组件是 Vue 中实现树状结构的常见方法。通过组件自身调用自身,可以轻松构建多层级树状结构。 <template> <ul…

vue 实现modal

vue 实现modal

Vue 实现 Modal 的方法 使用 Vue 原生组件 通过 Vue 的组件系统可以轻松实现 Modal。创建一个可复用的 Modal 组件,通过 props 控制显示与隐藏。 <templ…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…