uniapp验证码倒计时60s的实现

夙愿已清 提交于 2020-08-13 14:39:33

发送验证码时,不能让客户一直发送验证码,所以需要设置一个60s后才能发送一次;具体代码实现:因为app和其他app不太一样,所以需要选择以这样的方式展示是时间,但是js逻辑代码是一样的;;

data

show_again: 0, //  显示发送验证码||请稍后按钮
count: "", // 等待时间
timer: null, //定时器

html

<button  class="blue send" v-if="show_again==0" @click="sendCode">发送验证码</button>
<button  class="blue send" v-if="show_again==1" @click="sendCodeAgain">请稍后重试</button>

js

sendCode(){
	const count = 60;
	if (!this.timer) {
		this.count = count;
		_this.show_again = 1;
		this.timer = setInterval(() => {
			if (this.count > 0 && this.count <= count) {
				this.count--;
			} else {
				_this.show_again = 0;
				clearInterval(this.timer);
				this.timer = null;
			}
		}, 1000)
	}
},
sendCodeAgain() {
	const _this = this;
	uni.showToast({
		title:  `请稍后重试(${_this.count})`,
		icon: "none",
		duration: 1500
	})
},

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!