/*
* 封装验证码倒计时对象
* 调用规则,必须是input type="button"元素来调用
* 例如:
* <input type="button" class="get_code" value="获取验证码"></input >
* var newCode = new GetCode($('.get_code').get(0));
* newCode.sendCode();
* */
function GetCode (element){
this._init_(element);
}
GetCode.prototype = {
constructor : GetCode,
_init_ : function(element) {
this.clock = '';
this.nums = 60;
this.btn = element;
},
sendCode: function (){
this.btn.disabled = true; //将按钮置为不可点击
this.btn.value = this.nums + '秒后可重发';
var that = this
this.clock = setInterval(this.doLoop(that), 1000); //一秒执行一次
},
/*需要注意的地方*/
doLoop: function(that){
var fuc = function(){
that.nums--;
if(that.nums > 0) {
that.btn.value = that.nums + '秒后可重发';
} else {
clearInterval(that.clock); //清除js定时器
that.btn.disabled = false;
that.btn.value = '获取验证码';
that.nums = 60; //重置时间
}
}
return fuc;
}
}
验证码倒计时代码封装(对象封装)
最新推荐文章于 2024-06-17 10:51:35 发布