使用原生js实现一个60秒倒计时验证码
程序员文章站
2022-06-09 20:06:44
...
使用原生js实现一个60秒倒计时验证码
let getCode = document.querySelector('.getCode');
let tel = document.querySelector('.tel');
let error = document.querySelector('.error');
let time = 60;
let timer;
getCode.onclick = function(){
let name = tel.value;
if (name == "") {
error.innerHTML='手机号不能为空!';
setTimeout(function(){
error.innerHTML='';
}, 3000);
return;
}
else {
if (!(/^1[3|4|5|6|7|8|9]\d{9}$/.test(name))) {
error.innerHTML='手机号格式有误.';
setTimeout(function(){
error.innerHTML='';
}, 3000);
return;
}
}
clearInterval(timer);
timer = setInterval(function(){
time--;
getCode.innerHTML = time + "秒后重试";
getCode.style.background="url('images/login/retry.png')";
getCode.disabled = true;
if (time==0) {
time = 60;
clearInterval(timer);
getCode.innerHTML = "获取验证码";
getCode.disabled = false;
}
},1000);
}
推荐阅读