点击倒数发短信验证码——疑问2

此处短信验证码只涉“及点击按钮后倒数”,并没有考虑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

是有必要的,此处有疑问~


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值