其他分享
首页 > 其他分享> > 节流

节流

作者:互联网

 1 <!--
 2  * @Author: your TM_cc
 3  * @Date: 2021-10-31 19:10:34
 4  * @LastEditTime: 2021-10-31 19:15:51
 5  * @LastEditors: Please set LastEditors
 6  * @Description: In User Settings Edit
 7  * @FilePath: \节流.html
 8 -->
 9 <!DOCTYPE html>
10 <html lang="en">
11 <head>
12     <meta charset="UTF-8">
13     <meta http-equiv="X-UA-Compatible" content="IE=edge">
14     <meta name="viewport" content="width=device-width, initial-scale=1.0">
15     <title>节流-Throttling</title>
16 </head>
17 <body>
18 
19     <button id="button">节流</button>
20 
21     <script>
22 
23         function Throttling(func,wait){
24             let timeout;
25             return function () {
26                 if (!timeout){
27                     timeout = setTimeout(function () {
28                         func()
29                         timeout = null;
30                     },wait)
31                 }
32             }
33         }
34 
35         function handle(){
36             console.log(Math.random())
37         }
38 
39         document.getElementById('button').onclick = Throttling(handle,2000)
40 
41         //  点击两秒后显示一个随机数,两秒内不论点击多少次都不会在此产生一个随机数
42 
43     </script>
44 </body>
45 </html>

 

标签:function,handle,节流,Throttling,timeout,两秒
来源: https://www.cnblogs.com/tmccjs/p/15490469.html