| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <!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>
|