zheng пре 6 дана
родитељ
комит
b30be2c196
1 измењених фајлова са 52 додато и 0 уклоњено
  1. 52 0
      11.复习/6.节流.html

+ 52 - 0
11.复习/6.节流.html

@@ -0,0 +1,52 @@
+<!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;
+            font-size: 30px;
+            background-color: #00f;
+        }
+    </style>
+</head>
+
+<body>
+    <div id="box"></div>
+    <!-- 
+        节流:
+            点击事件 在事件触发后 等待一段时间在执行回调函数
+            在等待的时间内 若再次点击事件 则不执行 需要当前回调函数完成后在执行
+            高频的按钮点击事件 滚动加载
+    -->
+    <script>
+        const box = document.getElementById("box");
+        let x = 0;
+
+        function CountMain() {
+            box.innerHTML = ++x;
+        }
+
+        function throttle(fn, delay) {
+            let timer = null;
+            return function () {
+                if (!timer) {
+                    timer = setTimeout(function () {
+                        fn();
+                        timer = null;
+                    }, delay)
+                }
+            }
+        }
+        box.addEventListener("click", throttle(CountMain, 3000))
+    </script>
+</body>
+
+</html>