vue 發送短信驗證碼倒計時


引入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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM