验证码倒计时
<body>
<span id="sendCode" style="background: #ccc;padding: 5px;">发送验证码</span>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<script>
var sendCode = $("#sendCode");
function countTime(options) {
options = options || {};
var time = options.time || 30;
var callback = options.callback || function() {};
var timeEndCallback = options.timeEndCallback || function() {};
var next = function() {
if (time == 0) {
clearTimeout(timer);
timer = null;
timeEndCallback();
} else {
callback(time);
time--;
timer = setTimeout(next, 1000);
};
};
timer = setTimeout(next, 0);
};
function countTime1() {
var disabledClass = "disabledClass";
if (!sendCode.hasClass(disabledClass)) {
countTime({
callback: function(s) {
sendCode.html(s + "s后重新发送");
},
timeEndCallback: function() {
sendCode.removeClass(disabledClass);
sendCode.html("重发验证码");
}
});
}
sendCode.addClass(disabledClass);
}
sendCode.on("click", function() {
countTime1();
});
</script>
</body>