Vue獲取驗證碼倒計時60秒按鈕詳解

一、Vue驗證碼倒計時刷新

驗證碼是應用程序中常用的功能,倒計時60秒的按鈕是驗證碼常見的實現方式。在Vue中,可以使用定時器來實現倒計時刷新。

首先,在Vue組件中聲明一個變量代表剩餘時間,該變量的初始值為60(表示60秒)。另外,還需要聲明一個計時器,用於每隔一秒更新剩餘時間,直到剩餘時間為0。在計時器中,每一秒將剩餘時間減1,同時更新顯示在頁面上的倒計時。

  <template>
    <button @click="refreshCode()" :disabled="disabled">{{ textBtn }}</button>
  </template>

  <script>
    export default {
      data() {
        return {
          remainingTime: 60,
          timer: null
        }
      },
      computed: {
        disabled() {
          return this.remainingTime !== 60;
        },
        textBtn() {
          return this.disabled ? `${this.remainingTime}秒後可重新發送` : '獲取驗證碼';
        }
      },
      methods: {
        refreshCode() {
          this.remainingTime = 60;
          this.timer = setInterval(() => {
            this.remainingTime--;
            if (this.remainingTime === 0) clearInterval(this.timer);
          }, 1000);
        }
      },
      destroyed() {
        clearInterval(this.timer);
      }
    }
  </script>

在代碼中,使用disabled來控制按鈕是否可用。當剩餘時間不為60秒時,按鈕將變為灰色且無法點擊,表示還需要等待一段時間才能重新發送驗證碼。同時,在計時器結束後需要清除計時器,否則計時器將一直運行下去。

二、Vue發送驗證碼倒計時

發送驗證碼倒計時與驗證碼倒計時刷新的實現方式較為相似,不同之處在於發送驗證碼需要調用後端API,而刷新驗證碼則是直接在前端更新剩餘時間。

首先,在發送驗證碼的函數中,需要調用後端API並等待返回結果。當接收到成功的響應後,才開始啟動倒計時計時器。與刷新驗證碼不同的是,在啟動計時器之前,要將發送按鈕禁用掉。另外,在計時器結束後,也要將按鈕重新啟用。

  <template>
    <button @click="sendCode()" :disabled="disabled">{{ textBtn }}</button>
  </template>

  <script>
    export default {
      data() {
        return {
          remainingTime: 60,
          timer: null,
          disabled: false
        }
      },
      computed: {
        textBtn() {
          return this.disabled ? `${this.remainingTime}秒後重新獲取` : '獲取驗證碼';
        }
      },
      methods: {
        async sendCode() {
          if (this.disabled) return;
          this.disabled = true;
          try {
            // 調用後端API發送驗證碼
            // 等待成功響應
            this.timer = setInterval(() => {
              this.remainingTime--;
              if (this.remainingTime === 0) {
                clearInterval(this.timer);
                this.disabled = false;
              }
            }, 1000);
          } catch (error) {
            // 出錯處理
            this.disabled = false;
          }
        }
      },
      destroyed() {
        clearInterval(this.timer);
      }
    }
  </script>

三、Vue短訊驗證碼倒計時

短訊驗證碼是應用程序中常用的驗證碼類型之一。與獲取驗證碼不同的是,短訊驗證碼是通過手機平台發送而非後端API。同樣,短訊驗證碼倒計時也需要在發送成功後啟動計時器。

前端代碼如下:

  <template>
    <button @click="sendCode()" :disabled="disabled">{{ textBtn }}</button>
  </template>

  <script>
    export default {
      data() {
        return {
          remainingTime: 60,
          timer: null,
          disabled: false
        }
      },
      computed: {
        textBtn() {
          return this.disabled ? `${this.remainingTime}秒後重新獲取` : '獲取短訊驗證碼';
        }
      },
      methods: {
        async sendCode() {
          if (this.disabled) return;
          this.disabled = true;
          try {
            // 調用手機平台API發送短訊驗證碼
            // 等待發送成功
            this.timer = setInterval(() => {
              this.remainingTime--;
              if (this.remainingTime === 0) {
                clearInterval(this.timer);
                this.disabled = false;
              }
            }, 1000);
          } catch (error) {
            // 出錯處理
            this.disabled = false;
          }
        }
      },
      destroyed() {
        clearInterval(this.timer);
      }
    }
  </script>

四、Vue實現驗證碼倒計時

實現驗證碼倒計時需要考慮到倒計時的場景和需要調用的API或函數。倒計時可以出現在多個地方,例如通過短訊驗證碼登錄、發送系統消息、重設密碼等。因此,在實現驗證碼倒計時時應該盡量通用,方便在各種場景中復用。

首先,在Vue組件中聲明一個計時器timer和一個剩餘時間remainingTime。計時器用於定時更新剩餘時間,剩餘時間初始值為60。同時,還需要聲明一個參數controlled,用於判斷倒計時是否被控制。

在計時器中,每秒減1,直到為0。如果倒計時被控制,可以通過觸發自定義事件來控制倒計時。在每次更新剩餘時間時,判斷剩餘時間是否為0,如果為0則停止計時器。

  <template>
    <button @click="refreshCode()" :disabled="disabled">{{ remainingTime }}</button>
  </template>

  <script>
    export default {
      props: {
        controlled: {
          type: Boolean,
          default: false
        }
      },
      data() {
        return {
          remainingTime: 60,
          timer: null,
          disabled: true
        }
      },
      watch: {
        controlled(newVal) {
          if (newVal) clearInterval(this.timer);
          this.disabled = !newVal;
          this.remainingTime = 60;
        }
      },
      mounted() {
        if (!this.controlled) {
          this.timer = setInterval(() => {
            this.remainingTime--;
            if (this.remainingTime === 0) clearInterval(this.timer);
          }, 1000);
        }
      },
      methods: {
        refreshCode() {
          if (!this.controlled) return;
          this.$emit('refresh');
          clearInterval(this.timer);
          this.disabled = true;
          this.remainingTime = 60;
        }
      }
    }
  </script>

使用時,可以將controlled設置為true,並在父組件中監聽組件的refresh事件即可控制倒計時的開始和結束。代碼如下:

  <template>
    <div>
      <my-countdown :controlled="controlled" @refresh="resetCountdown" />
      <button @click="startCountdown">開始倒計時</button>
    </div>
  </template>

  <script>
    import MyCountdown from './MyCountdown.vue';

    export default {
      components: {
        MyCountdown
      },
      data() {
        return {
          controlled: false
        }
      },
      methods: {
        startCountdown() {
          this.controlled = true;
          setTimeout(() => {
            this.controlled = false;
          }, 5000);
        },
        resetCountdown() {
          this.controlled = false;
        }
      }
    }
  </script>

在上面的代碼中,通過點擊「開始倒計時」按鈕來控制倒計時的開始和結束。當按鈕被點擊時,將controlled設置為true,此時倒計時組件將不會自動開始計時器,直到監聽到了refresh事件,才會開始計數。5秒後我們又將controlled設置為false,此時組件又會變為自動計時的狀態。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/158939.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-19 18:56
下一篇 2024-11-19 18:56

相關推薦

  • Access執行按鈕的實現方法及應用場景

    本文將詳細介紹Access執行按鈕的實現方法及其在實際應用場景中的使用方法。 一、創建Access執行按鈕的方法 在Access中,創建執行按鈕的方法非常簡單。只需要按照以下步驟進…

    編程 2025-04-27
  • python運行按鈕在哪

    Python運行按鈕指的是在集成開發環境(IDE)中開發者用來運行代碼的按鈕,請看下面的代碼示例: print(“Hello, World!”) 如果這段代碼保存為名為hello_…

    編程 2025-04-27
  • 如何在LinearLayout中使按鈕居中

    在LinearLayout布局中,如果想要讓按鈕居中,那麼可以通過以下幾種方法實現。 一、gravity屬性 在LinearLayout中,可以使用gravity屬性將其子控件相對…

    編程 2025-04-27
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁盤中。在執行sync之前,所有的文件系統更新將不會立即寫入磁盤,而是先緩存在內存…

    編程 2025-04-25
  • 神經網絡代碼詳解

    神經網絡作為一種人工智能技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網絡的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網絡模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性傳感器,能夠同時測量加速度和角速度。它由三個傳感器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25
  • C語言貪吃蛇詳解

    一、數據結構和算法 C語言貪吃蛇主要運用了以下數據結構和算法: 1. 鏈表 typedef struct body { int x; int y; struct body *nex…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分佈式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25

發表回復

登錄後才能評論