uniapp-发送验证码(节流)

本文记录了在uniapp项目中实现发送验证码功能时使用节流技术的实践过程。通过代码示例,介绍了如何简单有效地应用节流防止频繁发送验证码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uniapp-发送验证码(节流)

做项目时在写发送验证码的时候忘记写节流,写完后测试时才发现,写的时候又突然忘记写法了,决定还是记下来为好,还算比较简单,一看就能看懂;

代码如下:

export default {
	data(){
		return{
			totalTime: 60,
			canClick: true, // 节流
			clockTimer: null, //定时器
			codeText: '获取验证码',
		}
	},
	methods: {
		onSendCode() {
				//this.disabledCode = true;
				if (!this.canClick) return // 节流 防止频繁访问接口
				this.canClick = false
				console.log('进入倒计时...')
				this.codeText = "重新获取(" + this.totalTime + 's)'
				let that = this
				that.clockTimer = setInterval(() => {
					that.totalTime--
					that.codeText = "重新获取(" + that.totalTime + 's)'
					if (that.totalTime < 0) {
						clearInterval(that.clockTimer)
						that.codeText = '发送验证码'
						that.totalTime = 60
						that.canClick = true
						//that.disabledCode = false;
					}
				}, 1000)
			},
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值