<div id="app">
<button @click="sendCode" :disabled="isSend">{{sendTip}}</button>
</div>
<script>
var vm = new Vue({
el:"#app",
data(){
return {
sendTip:'获取验证码',
isSend:false
}
},
methods:{
sendCode(){
this.time = 20;
this.timer();
},
timer(){
if(this.time>0){
this.isSend = true; //按钮不可点击
this.time--;
this.sendTip = this.time + 's后重新获取';
setTimeout(this.timer,1000) //自执行本身,每隔一秒调用一次
}else{
this.time = 0;
this.sendTip = '获取验证码';
this.isSend = false //按钮可以点击
}
}
}
})
</script>