小程序?qū)崿F(xiàn)簡單驗證碼倒計時
更新時間:2022年07月28日 08:39:07 作者:久伍伍
這篇文章主要為大家詳細介紹了小程序?qū)崿F(xiàn)簡單驗證碼倒計時,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本篇文章主要講關(guān)于小程序驗證碼倒計時的功能實現(xiàn),供大家參考,具體內(nèi)容如下


首先是wxml部分
<form bindsubmit="regist">
? ? <view class="vip-title">驗證碼</view>
? ? ? <input type="text" name="verifyCode" placeholder="驗證碼" value="{{verifyCode}}" style="width:310rpx" />
? ? ? <button class="captcha" bindtap="captcha" disabled="{{captchaDisabled}}" plain="true" disabled-class="disabled">{{captchaLabel}}</button>
? ? </view>
?</form>樣式部分:
/*提交按鈕*/
form button {
? ? margin: 30rpx;
? ? background: #09f;
? ? color: white;
}
/*文本框容器*/
.input-container {
? ? margin: 40rpx 60rpx;
? ? display: flex;
? ? flex-direction: row;
? ? justify-content: space-between;
? ? align-items: center;
? ? border-bottom: 1px solid #ddd;
? ? padding-bottom: 6rpx;
}
/*文本框本身*/
.input-container input {
? ? color: #999;
? ? flex: 1;
? ? height: 40px;
}
/*占位符樣式*/
.input-placeholder {
? ? color: #999;
}
/*清空按鈕*/
.input-container image {
? ? width: 22px;
? ? height: 22px;
}
.forgot {
? ? margin: 0 30rpx 40rpx 30rpx;
? ? text-align: right;
? ? font-size: 28rpx;
? ? color: #ccc;
}
.captcha {
? ? margin: 0 8rpx;
? ? color: #fff;
? ? fon-size: 25rpx;
? p t-a
.button[plain] {
? ? color: #09f;JS部分:
var timer = require('../../utils/timer.js');
Page({
? ? data: {
? ? ? ? verifyCode: '', //6617
? ? ? ? captchaLabel: '獲取驗證碼',
? ? ? ? seconds: timer.length,
? ? ? ? captchaDisabled: false
? ? },
? ? onLoad: function() {
? ? },
? ? captcha: function(e) {
? ? ? ? var param = {
? ? ? ? ? ? phone: this.data.phone
? ? ? ? };
? ? ? ? // 禁用按鈕點擊
? ? ? ? this.setData({
? ? ? ? ? ? captchaDisabled: true
? ? ? ? });
? ? ? ? // 立刻顯示重發(fā)提示,不必等待倒計時啟動
? ? ? ? this.setData({
? ? ? ? ? ? captchaLabel: timer.length + '秒后重新發(fā)送'
? ? ? ? });
? ? ? ? // 啟動以1s為步長的倒計時
? ? ? ? var interval = setInterval(() => {
? ? ? ? ? ? timer.countdown(this);
? ? ? ? }, 1000);
? ? ? ? // 停止倒計時
? ? ? ? setTimeout(function() {
? ? ? ? ? ? clearInterval(interval);
? ? ? ? }, timer.length * 1000);
? ? ? ? if (this.data.seconds == timer.length) {
? ? ? ? ? ? console.log('post');
? ? ? ? ? ? wx.showToast({
? ? ? ? ? ? ? ? title: '發(fā)送成功'
? ? ? ? ? ? });
? ? ? ? }
? ? },
})timer.js :
var length = 5;
function countdown(that) {
? ? console.log('count down');
? ? var seconds = that.data.seconds;
? ? console.log(seconds);
? ? var captchaLabel = that.data.captchaLabel;
? ? if (seconds <= 1) {
? ? ? ? captchaLabel = '獲取驗證碼';
? ? ? ? seconds = length;
? ? ? ? that.setData({
? ? ? ? ? ? captchaDisabled: false
? ? ? ? });
? ? } else {
? ? ? ? captchaLabel = --seconds + '秒后重新發(fā)送'
? ? }
? ? that.setData({
? ? ? ? seconds: seconds,
? ? ? ? captchaLabel: captchaLabel
? ? });
}
module.exports = {
? ? countdown: countdown,
? ? length: length
}以上就是獲取驗證碼功能的實現(xiàn)。
希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換
樹或者圖是個比較抽象的概念,并不存在這樣的數(shù)據(jù)類型。數(shù)組就比較簡單了,因此數(shù)組和樹的轉(zhuǎn)換可以理解為數(shù)組和對象之間的轉(zhuǎn)換。本文將用TypeScript實現(xiàn)數(shù)組和樹的相互轉(zhuǎn)換,感興趣的可以了解一下2022-06-06
JS實現(xiàn)數(shù)組扁平化的方法總結(jié)
數(shù)組扁平化相信不少朋友在一些面試中被問到過,這在我們?nèi)粘>幊讨幸彩且粋€常規(guī)操作,它需要我們將一個多維數(shù)組轉(zhuǎn)化成一個一維數(shù)組,所以,借著這篇文章,我們今天就一起來匯總一下幾種數(shù)組扁平化的方式,需要的朋友可以參考下2024-02-02
原生js獲取iframe中dom元素--父子頁面相互獲取對方dom元素的方法
下面小編就為大家?guī)硪黄鷍s獲取iframe中dom元素--父子頁面相互獲取對方dom元素的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
JavaScript 實現(xiàn)打印,打印預(yù)覽,打印設(shè)置
這篇文章主要介紹了JavaScript 實現(xiàn)打印,打印預(yù)覽,打印設(shè)置的方法及示例分享,需要的朋友可以參考下2014-12-12

