HTML
<button type="button" onclick="getCode();" id='getCode'>获取验证码</button>
JS
function getCode(){
var InterValObj; //timer变量,控制时间
var count = 3; //间隔函数,1秒执行
var curCount;//当前剩余秒数
var getCode = $("#getCode");
curCount = count;
//设置button效果,开始计时
getCode.attr("disabled", "true");
getCode.css('background', '#cccccc').css('border', '1px solid #cccccc');
getCode.html(curCount + "秒后获取");
InterValObj = window.setInterval(function () {
if (curCount == 1) {
window.clearInterval(InterValObj);//停止计时器
getCode.removeAttr("disabled");//启用按钮
getCode.removeAttr("style");
getCode.html("获取验证码");
} else {
curCount--;
getCode.html(curCount + "秒后获取");
}
}, 1000);
}
效果
代码简单而实用,感谢感谢