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