javascript之setInterval和clearInterval

 1 <!DOCTYPE html>
 2 <html lang="zh">
 3     <head>
 4         <meta charset="UTF-8">
 5         <meta http-equiv="X-UA-Compatible" content="IE=edge">
 6         <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7         <title>Document</title>
 8         <style>
 9             #timeDiv {
10                 font-size: 18px;
11                 height: 20px;
12                 line-height: 20px;
13             }
14         </style>
15 
16 
17     </head>
18     <body>
19         <script>
20             'user strict';
21             window.onload = function() {
22                 document.getElementById("startBtn").onclick = function() {
23                     // 显示时间函数
24                     function timeDisplay() {
25                         let timeStr = new Date().toLocaleString();
26                         document.getElementById("timeDiv").innerText = timeStr;
27                     }
28                     window.timeFlag = window.setInterval(timeDisplay, 1000);
29                 }
30                 document.getElementById("stopBtn").onclick = function() {
31                     window.clearInterval(window.timeFlag);
32                 }
33             }
34         </script>
35 
36         <div id="timeDiv"></div>
37         <button id="startBtn">显示时间</button>
38         <button id="stopBtn">显示暂停</button>
39 
40 
41     </body>
42 </html>

 

posted @ 2023-03-08 13:51  jason2018  阅读(42)  评论(0编辑  收藏  举报