为何for循环无用,直接显示1
for(times=60;times>0;times--){send.value=times+"秒后重新发送";}
for(times=60;times>0;times--){send.value=times+"秒后重新发送";}
2017-03-19
window.onload=function(){
var send=document.getElementById('send'),
times=60,
timer=null;
send.onclick=function(){
// 计时开始
this.disabled="disabled";
timer=setInterval(function(){
times--;
send.value=times+"秒后重试";
if(times==0){
send.disabled=false
clearInterval(timer);
send.value="请发送验证码";
times=60
}
},1000)
}
}
我觉得应该是因为send.value会直接显示最后的值,不会随着for循环改变value,你可以用一个定时器对send的value值进行改变,我在你的代码下面加了一个consol.log(times+"秒后重新发送");是可以在控制台输出60~1的语句的。
var send=document.getElementsByClassName('send')[0];
var times=60;
setInterval(time,100)
function time(){
times--;
send.value=times+'秒后重新发送';
(times<=0)&&(send.value='重新发送');
}举报