【JS】定时器的开启与关闭

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>定时器</title>
</head>
<body>
<button id="closedsq">关闭</button>
<script>

//间隔定时器:按照周期去执行
//延时定时器: 在固定的时间后执行
var time1=setInterval(function () {
    console.log('间隔定时器')
},1000)

var time2=setTimeout(function () {
    console.log('延时定时器')
},1000)
//关闭定时器 不区分定时器种类
// clearInterval()
// clearTimeout()
closedsq.onclick=function () {
    clearInterval(time1)
    clearInterval(time2)
}
</script>
</body>
</html>

 

posted @ 2022-07-10 11:12  木子欢儿  阅读(1004)  评论(0编辑  收藏  举报