var countdown=60;
function sendCode(obj) {
var mobile=jQuery("#mobile").val();
var re = /^1[3|4|5|7|8][0-9]{9}$/;
if(mobile==""||!re.test(mobile)){
jQuery("#testPhone").attr("style","display:block");
return;
}
if(countdown == 60){
jQuery.ajax({
url:"$!webPath/send_code.htm?mobile="+mobile,async:true
});
}
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.value="免费获取验证码";
countdown = 60;
return;
} else {
obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function(){sendCode(obj)},1000);
}
<input name="code" type="text" id="code" placeholder="请输入验证码">
<input type="button" id="btn" value="免费获取验证码" οnclick="sendCode(this)"/>