話不多說,直接上代碼:
var countdown = 60;// 倒計(jì)時60s定義
var settime = function (that) {
? if (countdown == 0) {
? ? that.setData({
? ? ? alreadyGetVerifyCode: false
? ? })
? ? countdown = 60;
? ? return;
? } else {
? ? that.setData({
? ? ? alreadyGetVerifyCode: true,
? ? ? coolingTime: `${countdown}s重新獲取`
? ? })
? ? countdown--;
? }
? setTimeout(function () {
? ? settime(that)
??}
? ? , 1000)
}
Page({
? /**
? * 頁面的初始數(shù)據(jù)
? */
? data: {
? ? alreadyGetVerifyCode:false,
? ? coolingTime:''
? },
/**
? * 獲取驗(yàn)證碼事件
? */
? getVerifyCodeAction:function(e) {
? ? if (!this.data.alreadyGetVerifyCode) {
? ? ? var that = this;
? ? ? that.setData({
? ? ? ? alreadyGetVerifyCode: (!that.data.alreadyGetVerifyCode)
? ? ? })
? ? ? settime(that);
? ? } else {
? ? ? console.log('驗(yàn)證碼CD');
? ? }
? }