此处短信验证码只涉“及点击按钮后倒数”,并没有考虑IP地址限制,限制多发,或限制点击频率等
代码如下:(以下代码可以运行,但死循环)
<script type="text/javascript">
var countdown=120;
function settime(obj) {
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.value="免费获取验证码";
countdown = 120;
return;
} else {
obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(obj) }
,1000)
}
</script>
<div>
<span>短信校验码:<i style="color:#fb7070">*</i></span>
<span><input type="text" placeholder="输入短信校验码" class="text" id="ReigScid" maxlength="4"></span>
<input type="button" id="btn" value="免费获取验证码" οnclick="settime(this)" class="btn" style="background-color:#CC9;float:left;" />
/div>
正确解决死循环的代码:
<script type="text/javascript">
var countdown = 20;
function settime(obj) {
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.value = "免费获取验证码";
countdown = 20;
} else {
obj.setAttribute("disabled", true);
obj.value = "重新发送(" + countdown + ")";
countdown--;
setTimeout(function () {
settime(obj)
}, 1000)
}
}
</script>
真正应用涉及获取对象时:
$(function () {
var util = {
wait: 90,
hsTime: function (that) {
_this = $(this);
if (_this[0].wait == 0) {
$('#hsbtn').removeAttr("disabled").val('重发短信验证码');
_this.wait = 90;
} else {
var _this = this;
$(that).attr("disabled", true).val('在' + _this.wait + '秒后点此重发');
_this.wait--;
setTimeout(function () {
_this.hsTime(that);
}, 1000)
}
}
}
此处用JQUERY获取对象时,会变成数组,
_this[0].wait
是有必要的,此处有疑问~