WeChat applet implements countdown for sending SMS verification code

WeChat applet implements countdown for sending SMS verification code

This article shares the specific code for the WeChat applet to send SMS verification code countdown for your reference. The specific content is as follows

Rendering

WXML File

<view class="container">
  <view class="userinfo">
    <image class="userinfo-avatar" src="../../images/timg.jpg" mode="cover"></image>
    <text class="userinfo-nickname">What</text>
  </view>
  <view class="wrap">
    <view class="tel">
      <input type="number" bindinput="bindTelInput" maxlength="11" 
         placeholder="Please enter your phone number"
         placeholder-style="color:#C3C6C4;"/>
    </view>
    <view class="ver-code">
      <view class="code">
        <input type="number" bindinput="bindCodeInput" maxlength="6"
               placeholder="Please enter the verification code"
               placeholder-style="color:#C3C6C4;"/>
      </view>
      <view class="getCode" bindtap="getCode" wx:if="{{countDownNum == 60 || countDownNum == -1}}">
        <button type="primary" plain="true">Get verification code</button>
      </view>
      <view class="getCode" wx:else>
        <button type="primary" plain="true">Re-acquire after {{countDownNum}}s</button>
      </view>
    </view>
  </view>
  <view class="btn-login" bindtap="login">Login</view>
</view>

JS file

//Get the application instance const app = getApp()

Page({

  /**
   * Initial data of the page */
  data: {
    phone: null, // Phone number code: null, // Phone verification code countDownNum: 60, // Countdown initial value },

  /**
   * Life cycle function--listen for page loading*/
  onLoad: function (options) {

  },

  /**
   * Life cycle function--monitor page display*/
  onShow: function () {
    
  },
  
  /**
   * Life cycle function - listen for the completion of the initial rendering of the page*/
  onReady: function () {

  },
  
  /**
   * User clicks on the upper right corner to share*/
  onShareAppMessage: function () {

  },
  // Input phone number bindTelInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // Input verification code bindCodeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  // Send mobile verification codegetCode: function () {
    if (!!this.data.phone) {
      if (!!(/^1[34578]\d{9}$/.test(this.data.phone))) {
        wx.showToast({
          title: "Sent successfully",
          icon: "none",
          duration: 1500
        })
        this.countDown()
      } else {
        wx.showToast({
          title: "Please enter a valid mobile phone number",
          icon: "none",
          duration: 1500
        })
      }
    } else {
      wx.showToast({
        title: "Please enter your mobile number",
        icon: "none",
        duration: 1500
      })
    }
  },
  /**
   * Verification code countdown*/
  countDown: function () {
    var _this = this
    var countDownNum = _this.data.countDownNum // Get the countdown initial value var timer = setInterval(function () {
      countDownNum -= 1
      _this.setData({
        countDownNum: countDownNum
      })
      if (countDownNum <= -1) {
        clearInterval(timer)
        // Unpin the code that the setInterval function will execute_this.setData({
          countDownNum: 60
        })
      }
    }, 1000)
  },
  // Mobile phone verification code login: function () {
    if (this.data.phone) {
      if (!!(/^1[34578]\d{9}$/.test(this.data.phone))) {
        if (this.data.code) {
          wx.showToast({
            title: "Login successful",
            icon: "none",
            duration: 1500
          })
        } else {
          wx.showToast({
            title: "Please enter the verification code",
            icon: "none",
            duration: 1500
          })
        }
      } else {
        wx.showToast({
          title: "Please enter a valid mobile phone number",
          icon: "none",
          duration: 1500
        })
      }
    } else {
      wx.showToast({
        title: "Please enter your mobile number",
        icon: "none",
        duration: 1500
      })
    }
  }
})

WXSS File

.userinfo {
  height: 240rpx;
  margin: 40rpx auto 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo .userinfo-avatar {
  width: 140rpx;
  height: 140rpx;
  margin: 20rpx;
  border-radius: 50%;
  border: 1rpx solid #dad5d5;
}

.userinfo .userinfo-nickname {
  color: #aaa;
}

.wrap {
  width: 630rpx;
  font-size: 32rpx;
  margin: 80rpx auto 120rpx;
}

.wrap .tel {
  width: 100%;
  height: 68rpx;
  border-bottom: 1rpx solid #DDE3EC;
  margin-bottom: 60rpx;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.wrap .ver-code {
  width: 100%;
  height: 68rpx;
  border-bottom: 1rpx solid #DDE3EC;
  display: flex;
  justify-content: space-between;
}

.wrap .ver-code .code {

}

.wrap .ver-code .getCode {
  min-width: 190rpx;
  height: 40rpx;
}

.wrap .ver-code .getCode button {
  width: 100%;
  height: 100%;
  font-size: 28rpx;
  font-weight: normal;
  line-height: 40rpx;
  background: #fff;
  color: #ffaa7f;
  border: none;
  padding: 0;
  margin: 0;
}

.btn-login {
  width: 588rpx;
  height: 88rpx;
  background: #ffaa7f;
  border-radius: 10rpx;
  text-align: center;
  line-height: 88rpx;
  font-size: 36rpx;
  font-weight: 500;
  color: #fff;
  margin: 0 auto;
}

.clickClass {
  background: #ea986c;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat applet realizes verification code countdown effect
  • WeChat applet implements verification code countdown
  • WeChat applet implements a 60s countdown to get the verification code on the phone
  • The registration page of the WeChat applet contains a countdown verification code and obtains user information
  • WeChat applet project practice verification code countdown function
  • WeChat applet implements verification code acquisition countdown effect
  • WeChat applet implements a 60s countdown to get the verification code
  • WeChat applet implements SMS verification code countdown

<<:  The difference and usage of datetime and timestamp in MySQL

>>:  Solution to the problem that the docker container cannot be stopped

Recommend

Summary of 50+ Utility Functions in JavaScript

JavaScript can do a lot of great things. This art...

Detailed explanation of Json format

Table of contents A JSON is built on two structur...

Detailed example of concatenating multiple fields in mysql

The MySQL query result row field splicing can be ...

Using Docker to create static website applications (multiple ways)

There are many servers that can host static websi...

A brief discussion on MySQL temporary tables and derived tables

About derived tables When the main query contains...

A few experiences in self-cultivation of artists

As the company's influence grows and its prod...

How to modify the mysql table partitioning program

How to modify the mysql table partitioning progra...

Detailed explanation of custom events of Vue components

Table of contents Summarize <template> <...

docker-maven-plugin packages the image and uploads it to a private warehouse

Table of contents 1. Introduction to docker-maven...

The difference between clientWidth, offsetWidth, scrollWidth in JavaScript

1. Concept They are all attributes of Element, in...

How to use vite to build vue3 application

1. Installation Tip: There is currently no offici...

SQL left join and right join principle and example analysis

There are two tables, and the records in table A ...

Solution to 1449 and 1045 exceptions when connecting to MySQL

Solution to 1449 and 1045 exceptions when connect...

Example of adding attributes using style in html

Add inline styles to the required links: Copy code...