VUE+elementUI實現(xiàn)表單發(fā)送驗證碼倒計時方法
1.HTML代碼部分
<el-form-item label="輸入驗證碼" prop="verificationCode">
<el-input v-model="accountSettingsForm.verificationCode" placeholder="請輸入驗證碼" style="width: 84%"></el-input>
<el-button icon="el-icon-mobile-phone" @click="send" style="width: 15%" type="success" :disabled="disabled=!show" >
<span v-show="show">獲取驗證碼</span>
<span v-show="!show" class="count">{{count}} s</span>
</el-button>
</el-form-item>
2.js代碼部分
const TIME_COUNT = 60; //更改倒計時時間
data(){
return {
show: true, // 初始啟用按鈕
count: '', // 初始化次數(shù)
timer: null,
}
},
methods:{
send(){
if (!this.timer) {
this.count = TIME_COUNT;
this.show = false;
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--;
} else {
this.show = true;
clearInterval(this.timer); // 清除定時器
this.timer = null;
}
}, 1000)
}
}
}