发送短信按钮倒计时

1,定时器执行的函数

var SMSbtnTiming = function () {
                    if (secondCount === 0) {
                        $self.addClass('enabled').removeClass('disabled');
                        $self.val('重新发送');
                        $self.removeAttr('disabled');
                        if (intervalHook) {
                            clearInterval(intervalHook);
                            intervalHook = null;
                        }

                    } else {
                        $self.val('重新发送(' + secondCount + 's)');
                        secondCount--;
                    }

                };

 

2,发送请求,并且操作成功后才执行倒计时

$.ajax({
                    url: changeBindServer + action,
                    type: 'GET',
                    dataType: 'jsonp',
                    data: {mobile: mobile},
                    success: function (data) {
                        if (data.result) {
                            $spanHint.removeClass(CodeErrorMsg_class);
                            $spanHint.show();
                            $spanHint.text('发送成功');
                            $self.addClass('disabled').removeClass('enabled');
                            $self.val('重新发送(60s)');
                            $self[0].disabled = 'disabled';//禁止按钮的点击默认动作
                            //$ChanjetNewCodeInput[0].disabled = "disabled";
                            intervalHook = setInterval(SMSbtnTiming, 1000);
                            enAbleNextBtn($inputTF);
                            $inputTF.removeAttr('disabled');
                            $inputTF.focus();
                        } else {
                            console.log(data);
                            var date = new Date();
                            console.log(date);
                            if (data.errorMessage.indexOf('绑定') != -1) {
                                setOldMobileMsg($oldMobileError, data.errorMessage);
                            } else {
                                $spanHint.text(data.errorMessage);//数量超出限制
                                $spanHint.addClass(CodeErrorMsg_class);
                                $spanHint.show();
                            }

                            //setTimeout(restoreSpanHit, 4000);
                        }
                        setTimeout(restoreSpanHit, 5000);
                    }
                });

 

3,定时器方法中的this指什么?

js代码:

var timing2= function () {
            console.log(this);
            console.log('timing');
            clearInterval(a);
        };
        a=setInterval(timing2, 2000);

 运行结果:

 

代码改为:

var obj={
            time2: function () {
                var timing2= function () {
                    console.log(this);
                    console.log('timing');
                    clearInterval(a);
                };
                a=setInterval(timing2, 2000);
            }
        };
        obj.time2();

 

结果也一样

 

解决方法:

抽取出来一个方法:

SMSbtnTimingCommon: function (me) {//定时器的方法
        //me._data=me._data||{};
        //console.log(me);
        var $self = $('#getAuthCodeBtn');
        if (me._data.bind.secondCount === 0) {
            var intervalHookTmp = me._data.bind.intervalHook;
            if (intervalHookTmp) {
                console.log('me._data.intervalHook:' + intervalHookTmp);
                clearInterval(intervalHookTmp);
                console.log('me._data.intervalHook:' + intervalHookTmp);
                console.log('stop timing');
                me._data.bind.intervalHook = null;
            }
            $self.html('获取验证码');
            sendCodeBtn.enable($self);
        } else {
            //console.log(me._data.secondCount);
            $self.html('重新获取(' + me._data.bind.secondCount + ')');
            me._data.bind.secondCount = me._data.bind.secondCount - 1;
        }

    }

 调用:

var SMSbtnTiming= function () {
            me.SMSbtnTimingCommon(me);
        };

 

 

 

你可能感兴趣的:(倒计时,倒计时剩余时间)