vue倒计时按钮

270 阅读1分钟

模板

 <div v-if="getVerif">
      <button @click="getVerifa">获取验证码</button>
    </div>
    <div v-else>
      {{v_second}}秒后获取
    </div>

方法

 getVerifa() {
        this.getVerif = false
        let fun = () => {
          if (this.v_second > 0) {
            this.v_second = this.v_second - 1
          }
          else {
            this.getVerif = true
            console.log('....')
            clearInterval(myVar)
            this.v_second = 10
          }
        }
        let myVar = setInterval(() => {
          fun()
        }, 1000);
      }