javascript - Verification code countdown
阿神
阿神 2017-05-19 10:33:06
0
1
567
<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?

阿神
阿神

闭关修行中......

reply all(1)
PHPzhong

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>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!