1 <html lang="en">
 2 
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 7     <title>一次定时器</title>
 8 </head>
 9 
10 <body>
11     <button id="btn">3秒后释放</button>
12     <button id="btn1">停止</button>
13     <script>
14         window.onload = function() {
15             //1.获取标签
16             var btn = document.getElementById('btn');
17             var btn1 = document.getElementById('btn1');
18             var timer = null;
19             btn.onclick = function() {
20                 clearTimeout(timer)
21                 timer = setTimeout(function() {
22                     alert('3秒释放')
23                 }, 3000)
24             }
25             btn1.onclick = function() {
26                 clearTimeout(timer)
27             }
28         }
29     </script>
30 </body>
31 
32 </html>