我這奇葩代碼,為啥倒數到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(){
?? ?????? // 計時開始? ?
????????? function countDown(){ ?
????????? send.value = times + "秒后重試";
????????? send.disabled=true;
????????? times--;
????????? if(times <= 0){
????????????? clearInterval(timer);
????????????? send.disabled=false;
????????????? times=5;
????????????? send.value = "發送驗證碼";
????????? }
????????? } ?
????????? timer=setInterval(countDown,1000);
?? ???? }
?? ?}
?? ?</script>
</head>
<body>
?? ?<input type="button" id="send" value="發送驗證碼">
</body>
</html>
2016-10-06
if(times <= 0){
? ? ? ? ? ? ?clearInterval(timer);
? ? ? ? ? ? ?send.disabled=false;
? ? ? ? ? ? ?times=5;
? ? ? ? ? ? ?send.value = "發送驗證碼";
? ? ? ? ?}else{
? ? ? ? ? send.value = times + "秒后重試";
? ? ? ? ?send.disabled=true;
? ? ? ? ?times--;
? ? ? ? ?}
把之前執行的語句放在else中,就行
2016-01-08
times==1時,按鈕的值變成了"1秒后重試",只不過一閃而過,馬上就執行了下面的語句,times--,變為0,執行if里面的語句,按鈕的值瞬間變成了"發送驗證碼"。