我這奇葩代碼,為啥倒數(shù)到2就停了
<!doctype html>
<html>
<head>
??? <meta charset="UTF-8">
?? ?<title>Document</title>
?? ?<script type="text/javascript">
?? ?window.onload=function(){
?? ???? var send=document.getElementById('send'),
?? ???????? times=5,
?? ???????? timer=null;
?? ???? send.onclick=function(){
?? ?????? // 計(jì)時(shí)開始? ?
????????? function countDown(){ ?
????????? send.value = times + "秒后重試";
????????? send.disabled=true;
????????? times--;
????????? if(times <= 0){
????????????? clearInterval(timer);
????????????? send.disabled=false;
????????????? times=5;
????????????? send.value = "發(fā)送驗(yàn)證碼";
????????? }
????????? } ?
????????? timer=setInterval(countDown,1000);
?? ???? }
?? ?}
?? ?</script>
</head>
<body>
?? ?<input type="button" id="send" value="發(fā)送驗(yàn)證碼">
</body>
</html>
2016-10-06
if(times <= 0){
? ? ? ? ? ? ?clearInterval(timer);
? ? ? ? ? ? ?send.disabled=false;
? ? ? ? ? ? ?times=5;
? ? ? ? ? ? ?send.value = "發(fā)送驗(yàn)證碼";
? ? ? ? ?}else{
? ? ? ? ? send.value = times + "秒后重試";
? ? ? ? ?send.disabled=true;
? ? ? ? ?times--;
? ? ? ? ?}
把之前執(zhí)行的語(yǔ)句放在else中,就行
2016-01-08
times==1時(shí),按鈕的值變成了"1秒后重試",只不過(guò)一閃而過(guò),馬上就執(zhí)行了下面的語(yǔ)句,times--,變?yōu)?,執(zhí)行if里面的語(yǔ)句,按鈕的值瞬間變成了"發(fā)送驗(yàn)證碼"。