zheng 14 ساعت پیش
والد
کامیت
c393eb5edd
1فایلهای تغییر یافته به همراه51 افزوده شده و 0 حذف شده
  1. 51 0
      11.复习/10.节流.html

+ 51 - 0
11.复习/10.节流.html

@@ -0,0 +1,51 @@
+<!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>
+        #box {
+            width: 300px;
+            height: 300px;
+            text-align: center;
+            line-height: 300px;
+            color: #ff0;
+            background: #00f;
+        }
+    </style>
+</head>
+
+<body>
+    <!-- 
+        节流:
+        点击事件 在事件触发后 等待一段时间在执行回调函数
+        在等待的时间内 若再点击事件 则不执行 需要完成回调函数后才会执行
+        高频的按钮点击事件
+        滚动加载
+    -->
+    <div id="box"></div>
+    <script>
+        var box = document.getElementById("box");
+        let x = 0;
+        function CountMain() {
+            box.innerText = x++;
+        }
+        // 防抖
+        function throttle(fn, delay) {
+            var timer = null;
+            return function () {
+                if (!timer) {
+                   timer = setTimeout(function () {
+                        fn();
+                        timer = null;
+                    }, delay)
+                }
+            }
+        }
+        box.addEventListener('click', throttle(CountMain, 5000))
+    </script>
+</body>
+
+</html>