search

Home  >  Q&A  >  body text

javascript - Verification code countdown

<input type="button" id="btn" value="免费获取验证码" onclick="settime(this)" /> 
<script type="text/javascript"> 
var countdown=60; 
function settime(val) { 
if (countdown == 0) { 
val.removeAttribute("disabled");  
val.value="免费获取验证码"; 
countdown = 60; 
} else { 
val.setAttribute("disabled", true); 
val.value="重新发送(" + countdown + ")"; 
countdown--; 
} 
setTimeout(function() { 
settime(val) 
},1000) 
} 
</script> 

Could you please tell me that when the time times out after using this code, the method will automatically loop, but after deleting the setTimeout method, the code function cannot be realized. How to solve it?

阿神阿神2747 days ago584

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-19 10:35:06

    Add a return ; and that’s it. I gave it a try.
    <input type="button" id="btn" value="Get verification code for free" onclick="settime(this)" />
    <script type="text/javascript">

    var countdown=60; 
    function settime(val) { 
    if (countdown == 0) { 
        val.removeAttribute("disabled");  
        val.value="免费获取验证码"; 
        countdown = 60; 
        return ; // 结束循环
    } else { 
        val.setAttribute("disabled", true); 
        val.value="重新发送(" + countdown + ")"; 
        countdown--; 
    } 
    setTimeout(function() { 
        settime(val) 
        },1000) 
    } 

    </script>

    reply
    0
  • Cancelreply