短信验证码倒计时【vue】[html+js]
方式一:短信验证码倒计时 vue
// html <el-form-item v-show="codeCheck"> <el-input v-model="formLoginData.message" maxlength="6" type="text" class="msgInput" placeholder="请输入验证码" @keyup.enter.native="verificationCode" /> <el-button type="primary" class="msgBtn" @click="sendSms">{{ sendContent }}</el-button> </el-form-item> //data data() { return { sendContent: '获取验证码', smsWait: 0, sendClass: false, } }, // 方法 methods: { // 发送验证码 sendSms() { var that = this if (that.smsWait !== 0) { return false } console.log(that.formLoginData.phone) if (isEmpty(that.formLoginData.phone)) { this.$message({ message: '请输入手机号', type: 'error' }) return false } if (!isMobile(that.formLoginData.phone)) { this.$message({ message: '手机号格式不正确', type: 'error' }) return false } that.smsWait = 60 that.waitSms() that.smsInterval = setInterval(function() { that.waitSms() }, 1000) }, // 验证码倒计时 waitSms() { var that = this that.smsWait-- var msgBtn = document.querySelector('.msgBtn') if (that.smsWait === 0) { clearInterval(that.smsInterval) that.sendContent = '重新获取' that.sendClass = false msgBtn.style.backgroundColor = '#1873f9' msgBtn.style.borderColor = '#1873f9' msgBtn.style.cursor = 'pointer' } else { that.sendClass = true that.sendContent = that.smsWait + '秒后重新获取' msgBtn.style.backgroundColor = '#aecdfa' msgBtn.style.borderColor = '#aecdfa' msgBtn.style.cursor = 'default' } }, // 手机号验证 verificationCode() { if (isEmpty(this.formLoginData.phone)) { this.$message({ message: '请输入手机号', type: 'error' }) return false } } }
方式二:短信验证码倒计时 [纯html+js]
/* 短信验证码倒计时*/ var send=document.getElementById('sendMsg'), times=60, timer=null; send.onclick=function(){ // 计时开始 var that = this; this.disabled=true; timer = setInterval(function(){ times --; that.value = times + "秒后重试"; if(times <= 0){ that.disabled =false; that.value = "发送验证码"; clearInterval(timer); times = 60; } },1000); } // html <input type="button" class="divInput msgBtn" id="sendMsg" value="发送短信验证码"/>
smile
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」