清除Interval
Tutorial: DOM操作
Category: JS
Published: 2026-04-07 13:58:26
Views: 20
Likes: 0
Comments: 0
clearInterval
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<button class="begin">开启定时器</button>
<button class="stop">停止定时器</button>
<script>
var begin = document.querySelector(".begin");
var stop = document.querySelector(".stop");
var timer = null;
begin.addEventListener("click", function () {
timer = setInterval(function () {
console.log("ni hao ma");
}, 1000);
});
stop.addEventListener("click", function () {
clearInterval(timer);
});
</script>
</body>
</html>