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