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