87,914
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
</style>
</head>
<body>
<button class= 'clear'>clear</button>
<script>
var btn = document.querySelector('button');
var timer= function fn() {
alert('hello');
};
setInterval(fn, 1000);
btn.addEventListener('click',function(){
clearIntervar('timer');
})
</script>
</body>
</html>
<script>
var btn = document.querySelector('button');
var timer= function fn() {//局部 外面访问不到 和匿名类一样
alert('hello');
};
setInterval(fn, 1000);//你可以用 timer 来替换拿不到的fn
btn.addEventListener('click',function(){
clearIntervar('timer');
})
</script>