代碼在自己的編輯器上跑起來了
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- 任務(wù)
第一步、獲取按鈕、綁定事件、設(shè)置定時(shí)器變量和計(jì)時(shí)變量
第二步、添加定時(shí)器,每隔1秒鐘計(jì)時(shí)減 1,直至當(dāng)計(jì)時(shí)小于等于 0 時(shí)清除定時(shí)器,按鈕恢復(fù)為“發(fā)送驗(yàn)證碼”,否則顯示為“X秒后重試” -->
<script type="text/javascript">
window.onload = function () {
var send = document.getElementById('send'),
times = 60,
timer = null;
send.onclick = function () {
// 計(jì)時(shí)開始
timer =setInterval(function(){
send.value=times+"秒后重試"
document.getElementById("send").disabled=true;
times--;
if(times<=0){
clearInterval(timer);
document.getElementById("send").disabled=false;
send.value="發(fā)送驗(yàn)證碼";
}
},1000);
}
}
</script>
</head>
<body>
<input type="button" id="send" value="發(fā)送驗(yàn)證碼">
</body>
</html>
2019-05-29
在設(shè)置定時(shí)器之前,要先清除準(zhǔn)備執(zhí)行的定時(shí)器