vue 发送短信验证码倒计时

本文介绍了一个基于Vue.js的自定义倒计时按钮组件实现方法。该组件用于输入框中,例如发送验证码操作,通过点击按钮触发倒计时功能,并在指定秒数后重新激活按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入iview.css/vue.js/iview.js

HTML:
<div id="main" class="main">
      <i-Input class="code"  size="large">
       <timer-btn slot="append" @click.native="sendCode()" :disabled="disabled" ref="btn" :second="60"></timer-btn>
      </i-Input>
  </div>

JS:
<script type="text/javascript">
  Vue.component('timerBtn', {
  template: '<i-Button :disabled="disabled || time > 0">{{ text }}</i-Button>',
  props: {
    second: {
      type: Number,
      'default': 60 
    },
    disabled: {
      type: Boolean,
      'default': false
    }
  },
  // 初始化时间
  data: function() {
    return {
      time: 0
    };
  },
  methods: {
    run: function() {
      this.time = this.second;
      this.timer();
    },
    stop: function(){
         this.time = 0;
         this.disabled = false;
    },
    timer: function() {
      if (this.time > 0) {
        this.time--;
        setTimeout(this.timer, 1000);
      }
    }
  },
  computed: {
    text: function() {
      return this.time > 0 ? this.time + 's再获取' : '获取验证码';
    }
  }
}); 



var vm = new Vue({
    el:'#main',
    data: {
      disabled: false
    },
    methods:{
        sendCode:function(){
            vm.$refs.btn.run(); //启动倒计时
            vm.disabled = false;
        }
    }
});
 </script>

 

转载于:https://www.cnblogs.com/gaomanito/p/9505816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值