獲取驗證碼 var countdown = 60; var but = document.getElementById('time'); but.addEventListener('click', function (e) { setTime(this); }) function setTime (... ...
<button id="time">獲取驗證碼</button>
var countdown = 60; var but = document.getElementById('time'); but.addEventListener('click', function (e) { setTime(this); }) function setTime (elem) { if (countdown === 0) { elem.innerHTML = "獲取驗證碼"; countdown = 60; elem.disabled = false; clearTimeout(anima); } else { countdown--; elem.innerHTML = "重新獲取(" + countdown + ")"; elem.disabled = true; var anima = setTimeout(function () { setTime(elem); }, 1000); } }