代碼只能執行一次如何修改?
<!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>
2019-02-24
你可以這樣:
??? 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);
?? ???? }
?? ?}
2016-12-16