<input type="button" id="btn" value="免費(fèi)獲取驗(yàn)證碼" onclick="settime(this)" />
<script type="text/javascript">
var countdown=60;
function settime(val) {
if (countdown == 0) {
val.removeAttribute("disabled");
val.value="免費(fèi)獲取驗(yàn)證碼";
countdown = 60;
} else {
val.setAttribute("disabled", true);
val.value="重新發(fā)送(" + 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?
閉關(guān)修行中......
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="免費(fèi)獲取驗(yàn)證碼";
countdown = 60;
return ; // 結(jié)束循環(huán)
} else {
val.setAttribute("disabled", true);
val.value="重新發(fā)送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(val)
},1000)
}
</script>