zoukankan      html  css  js  c++  java
  • 防抖和节流函数

      //防抖函数
            function debounce (fn, delay) {
                let timer = null;
                return function () {
                  //接收this,该this当前指向dom元素
                  const that = this;
                  //接收参数
                  const args = arguments;
                  //清除上次定时
                  clearTimeout(timer);
                  //开启定时
                  timer = setTimeout(() => {
                    //改变this指向,并把event传回去
                    fn.apply(that, args);
                  }, delay);
                }
            }


            //节流函数
            function throttle (fn, delay) {
              //定义上次执行时间
              let lastTime = 0;
              return function () {
                //当前时间
                let nowTime = Date.now();
                //小于指定时间就直接return
                if ((nowTime - lastTime) < delay)return;
                fn.apply(this, arguments);
                lastTime = nowTime;
              }
            }

  • 相关阅读:
    WPF MarkupExtension
    WPF Binding小数,文本框不能输入小数点的问题
    WPF UnhandledException阻止程序奔溃
    .Net Core的总结
    C#单元测试
    Csla One or more properties are not registered for this type
    unomp 矿池运行问题随记
    矿池负载运行监测记录
    MySql 数据库移植记录
    后台服务运行后无故停止运行,原因不明
  • 原文地址:https://www.cnblogs.com/wangsai-666/p/12038252.html
Copyright © 2011-2022 走看看