添加定时器+关闭定时器
<body>
<button>获取验证码</button>
<script>
let btn = document.querySelector('button')
let timeCount = 6
btn.addEventListener('click', function () {
let timeId = setInterval(function () {
timeCount--
btn.innerText = `倒计时${timeCount}s`
btn.disabled = true
if (timeCount == -1) {
clearInterval(timeId)
btn.disabled = false
btn.innerText = `获取验证码`
}
}, 1000)
})
</script>
</body>
自动切换图片
<body>
<div>
<img src="./images/b01.jpg" alt="" />
<p>第1张图片</p>
</div>
<script>
let img = document.querySelector('img')
let p = document.querySelector('p')
let index = 1
setInterval(function () {
index++
if (index == 10) {
index = 1
}
let path = `./images/b0${index}.jpg`
img.src = path
p.innerText = `第${index}张图片`
}, 2000)
</script>
</body>