zheng 13 часов назад
Родитель
Сommit
5440b82392
1 измененных файлов с 50 добавлено и 0 удалено
  1. 50 0
      11.复习/9.防抖.html

+ 50 - 0
11.复习/9.防抖.html

@@ -0,0 +1,50 @@
+<!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>
+    <!-- 
+        防抖:
+        点击事件 在事件触发后 等待一段时间在执行回调函数
+        在等待的时间内 若再点击事件 重新触发事件 重新计时
+        搜索框输入
+        滚动事件
+        resize窗口大小
+    -->
+    <div id="box"></div>
+    <script>
+        var box = document.getElementById("box");
+        let x = 0;
+        function CountMain() {
+            box.innerText = x++;
+        }
+        // 防抖
+        function debounce(fn, delay) {
+            var timer = null;
+            return function () {
+                if (timer) clearTimeout(timer);
+                timer = setTimeout(function () {
+                    fn();
+                }, delay)
+            }
+        }
+        box.addEventListener('click', debounce(CountMain, 5000))
+    </script>
+</body>
+
+</html>