12只怕有心人
2016-12-12 21:11
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input type="button" id="send" value="发送验证码">
<script type="text/javascript">
window.onload=function(){
var send=document.getElementById('send'),
times=60,
timer=null;
send.onclick=function(){
timer = setInterval(countDown,1000);
}
function countDown(){
times-=1;
send.setAttribute('disabled','disabled');
if(times<0){
clearInterval(timer);
send.setAttribute("value","重新发送验证码");
send.removeAttribute('disabled');
}
else
{
send.setAttribute("value",times+"秒后重试");
}
}
}
</script>
</body>
</html>
你可以这样:
window.onload=function(){
var send=document.getElementById('send'),
times=60,
timer=null;
send.onclick=function(){
// 计时开始
timer = setInterval(function(){
send.value = times + "秒后重试";
send.disabled = "disabled";
times--;
if(times == 0){
send.value = "发送验证码";
send.disabled = "";
clearInterval(timer);
times = 60;
}
},1000);
}
}
if(times<0){ clearInterval(timer); send.setAttribute("value","重新发送验证码"); send.removeAttribute('disabled'); 这里加 // times=60; // 第二次无效是因为times已经是-1了。直接成立if条件停止了。要重置times为60. }else
Tab选项卡切换效果
65469 学习 · 533 问题
相似问题