• 节流


    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>节流函数</title>
      </head>
      <body>
        <script>
          let a = `如果持续的去触发事件,每隔一段时间,只执行一次事件;`;
          function throttle1(func, delay) {
            let old = 0;
            return function () {
              let context = this;
              let args = arguments;
              let now = Date().now;
              if (now - old > delay) {
                func.apply(context, args);
                old = now;
              }
            };
          }
          function throttle2(func, delay) {
            let timer;
            return function () {
              let context = this;
              let args = arguments;
              if (!timer) {
                timer = setTimeout(function () {
                  func.apply(context, args);
                  timer = null;
                }, delay);
              }
            };
          }
          function throttle3(func, delay) {
            let timer;
            let old = 0;
            return function () {
              let context = this;
              let args = arguments;
              let now = Date().now;
              if (now - old > delay) {
                if (timer) {
                  clearTimeout(timer);
                  timer = null;
                }
                func.apply(context, args);
                old = now;
              } else if (!timer) {
                timer = setTimeout(function () {
                  now = Date().now;
                  func.apply(context, args);
                  timer = null;
                }, delay);
              }
            };
          }
          function throttle4(func, delay) {
            let timer;
            let old = 0;
            return function () {
              let context = this;
              let args = arguments;
              let now = Date().now;
              if (now - old > delay) {
                if (timer) {
                  clearTimeout(timer);
                  timer = null;
                }
                func.apply(context, args);
                old = now;
              } else if (!timer) {
                timer = setTimeout(function () {
                  now = Date().now;
                  func.apply(context, args);
                  timer = null;
                }, delay);
              }
            };
          }
          let a = `DOM元素的拖拽;射击游戏;计算鼠标移动的距离;监听scroll滚动;`;
        </script>
      </body>
    </html>
  • 相关阅读:
    解决ffmpeg打开流各种超时问题
    ffmpeg函数使用
    如何从AVFrame::data【0】里获取RGB24数据和YUYV422数据
    ffmpeg取rtsp流时av_read_frame阻塞的解决办法
    FFMPEG实时解码网络视频流(回调方式)
    JavaScript 演练(7). 赋值与引用
    JavaScript 演练(5). 模拟类
    曾经对 TMemoryStream.Memory 错误的理解
    JavaScript 演练(10). 谁的 this ?
    JavaScript 演练(6). 函数的定义与自执行
  • 原文地址:https://www.cnblogs.com/pengxiangchong/p/16216432.html
Copyright © 2020-2023  润新知