美文网首页
vue验证码60秒倒计时功能

vue验证码60秒倒计时功能

作者: 一名有马甲线的程序媛 | 来源:发表于2018-03-15 17:28 被阅读38次
打开页面开始倒计时:

html页中只需要获取倒计时的数{{countDownNum}},就ok啦~
js:

<script>
export default{
    data(){
        return{
            countDownNum:3,
            timer:null
        }
    },
    methods:{
        countDown:function(){
            this.timer=setInterval(() => {
                this.countDownNum--;
                if(this.countDownNum<=0){
                    clearInterval(this.timer);
                }
            },1000);
        }
    },
    mounted:function(){
        this.countDown()
    }
}
</script>
点击开始倒计时:

HTML:

<template lang="html">
<div>
  <span v-show="show" @click="getCode">获取验证码</span>
  <span v-show="!show" class="count">{{count}} s</span>
</div>
</template>

js:

<script>
export default {
  data(){
    return {
     show: true,
     count: '',
     timer: null
    }
 },
  methods:{
    getCode(){
     const TIME_COUNT = 3;
     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)
      }
    }  
  }
}
</script>

点击查看原文

相关文章

网友评论

      本文标题:vue验证码60秒倒计时功能

      本文链接:https://www.haomeiwen.com/subject/jebhqftx.html