vue倒计时

<template>

      <button @click="getVerificationCode" :disabled="disabled">

                <span v-if="sending">获取验证码</span>

                <span v-else>{{second}}秒后重发</span>

        </button>

<template>

<script>

      export default {

            data(){

                sending: true,      //是否发送验证码

                disabled: false,   //是否禁发验证码

                second: 60,        //倒计时间

            },

            methods:{

                 // 获取验证码

                  getVerificationCode() {

                        if (!this.sending)

                        return;

                        this.sending = false;

                        this.disabled = true;

                        this.timeDown();

                  },

                  timeDown() {

                        let result = setInterval(() => {

                        --this.second;

                        if (this.second < 0) {

                              clearInterval(result);

                              this.sending = true;

                              this.disabled = false;

                              this.second = 60;

                      }

                      }, 1000);

                },

            }

      }

</script>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容