1-1 3s倒计时
<div id="app">
3
</div>
<script>
var num =3;
var app = document.getElementById("app");
// function go(){
// setTimeout(()=>{
// num--;
// if(num>=0){
// go();
// app.innerHTML = num;
// }
// },1000)
// }
// go();
var timer = setInterval(()=>{
num--;
console.log(num)
if(num>=0){
app.innerHTML = num;
}else{
clearInterval(timer)
}
},1000)
</script>
1-2 按钮5s倒计时
<button id="btn">发送验证码</button>
<script>
var num =3;
var btn = document.getElementById("btn");
var timer;
btn.onclick = function(){
btn.disabled = true;
btn.innerHTML = num;
timer = setInterval(()=>{
num--;
if(num>=0){
btn.innerHTML = num;
}else{
btn.innerHTML = "发送验证码";
clearInterval(timer)
btn.disabled = false;
num=3;
}
},1000)
}
</script>