<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
setInterval(function(){ console.log("HI Major")},500)
</script>
</body>
</html>
function test(){ console.log("HI Major")}
setInterval(test,500)
关闭定时器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function test(){ console.log("HI Major")}
let x1 = setInterval(test,500)
console.log(x1)
let x2 = setInterval(test,600)
console.log(x2)
clearInterval(x2)
</script>
</body>
</html>
实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button class="btn" disabled>待点击(5)</button>
<script>
const btn = document.querySelector('.btn')
let i =5
let interId = setInterval(() => {
i--
btn.innerHTML= `待点击 (${i})`
if(i == 0){
btn.disabl
clearInterval(interId)
btn.disabled = false
btn.innerHTML = "可点击"
}
}, 1000);
</script>
</body>
</html>