countDown.js(时间减):
function CountDown(options) {
if ($.isPlainObject(options)) {
$.extend(this, options);
} else if ($.isNumeric(options)) {
this.time = options;
} else {
this.time = 0;
}
}
CountDown.prototype = {
start: function() {
return this.clear().run();
},
run: function() {
var S = this;
S.time--;
if (S.time > 0) {
S.id = setTimeout(function() {
S.change();
S.run();
}, 1000);
} else {
S.end();
}
return this;
},
end: function() {
return this;
},
change: function() {
return this;
},
reset: function(time) {
this.clear().time = time;
return this;
},
clear: function() {
clearTimeout(this.id);
return this;
}
};
return CountDown;
页面调用:
var doneFn = function (res) { btn.addClass('disable').text('60秒'); startCountDown(btn); }, failFn = function (code, json) { setTimeout(function () { if (code === 7) { btn.removeClass('disable').text('发送验证码'); } else { btn.removeClass('disable').text('重新发送'); } }, 1000); };