手机验证码倒计时(防重复点击)

获取手机验证码的倒计时

封装函数

获取手机验证码的倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<input type="button" value="获取验证码" id="btn1">
window.onload = function(){
var wait=60;
function time(o) {
if (wait == 0) {
o.removeAttribute("disabled");
o.value="获取验证码";
wait = 60;
} else {
o.setAttribute("disabled", true);
o.value="重新发送(" + wait + ")";
wait--;
setTimeout(function(){
time(o)
},1000);
}
}
document.getElementById("btn").onclick=function(){time(this);}
};

封装函数(jQuery)

获取手机验证码的倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<input type="button" value="获取验证码" id="btn1">
$(function(){
var wait=5;
function time(el){console.log(wait);
if(wait==0){
el.removeAttr('disabled');
el.val('重新获取');
wait=5;
}else{
el.attr('disabled',true);
el.val('稍等片刻('+ wait +')');
wait--;
setTimeout(function(){
time(el);
},1000);
}
}
$('#btn1').on('click',function(){
var self=$(this);
time(self);
});
});

坚持原创技术分享,您的支持将鼓励我继续创作!