uniapp实现发送验证码倒计时效果

<button type="primary"  @click='getVerificationCode' :disabled='disabled'>{{btntxt}}</button>
<script>
    import Vue from 'vue';
    export default Vue.extend({
        data() {
            return {
                btntxt: '发送验证码',
                seconds: 60,
                disabled: false,
            }
        },
        methods: {
            getVerificationCode() {
                const timer = setInterval(() => {
                    this.btntxt = this.seconds + '秒再试';
                    this.seconds--;
                    this.disabled = true;
                    if(this.seconds === 0){
                        clearInterval(timer);
                        this.btntxt = '发送验证码';
                        this.seconds = 60;
                        this.disabled = false
                    }
                }, 1000);
                
            }
        },
        
    });
</script>

 

posted @ 2021-09-24 10:59  赵永强  阅读(762)  评论(2编辑  收藏  举报