vue短信验证码倒计时

我也不知道关于前端的短信验证码有什么插件、模板之类的,项目需求,就搞了一个,注释已标,再调用接口的话就自己处理吧。直接上代码

<template>
    <div>
      <button v-show="Verification" @click="handleClick">点击获取验证码</button>
      <button v-show="!Verification"><span>{{timer}}</span>秒后重新获取</button>
    </div>
</template>

<script>
    export default {
      data(){
        return{
          Verification:true,    //通过v-show控制显示获取还是倒计时
          timer:60      //定义初始时间为60s
        }
      },
      methods:{
        handleClick(){
          this.Verification = false;      //点击button改变v-show的状态
          let auth_timer = setInterval(()=>{  //定时器设置每秒递减
            this.timer--;        //递减时间
            if(this.timer<=0){  
              this.Verification = true;    //60s时间结束还原v-show状态并清除定时器
              clearInterval(auth_timer)
            }
          },1000)
        }
      }
    }
</script>

<style lang="less">
button{
  padding: 10px 20px;
  border:1px solid #000
}
</style>

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值