JQ版
var countdown=180;
function settime(obj) {
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.value="获取验证码";
countdown = 180;
return;
} else {
obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(obj) }
,1000)
}
纯JS版(手机端)
var countdown=60;
function settime(id) {
var obj = document.getElementById(id);
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.innerText="获取验证码";
countdown = 60;
return;
} else {
obj.setAttribute("disabled", true);
obj.innerText = "重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(id) }
,1000)
}
扩展操作
var i = s;
var t = setInterval(function () {
i--
if (i == 1) {
clearInterval(t)
}
}, 1000);