SOURCE

//防抖
function fd(fn,wait){
    let time
    if(time){
        clearTimeout(time)
    }
    time =   setTimeout(fn,wait)
}
// 定义防抖函数
const debounce = function (fn, wait, immediate) {
  // 自由变量,debounce执行完成被释放,time也不会被释放
  let time;
  // 返回一个闭包,接受参数
  return function (...args) {
    // 保存闭包被调用时的this
    const this_ = this;
    // 清除上一次的定时器
    if (time) {
      clearTimeout(time);
    };
    // 配置开关
    if (immediate) {
      const action = !time;
      // time没置空前因为time存在,所以fn不会执行
      time = setTimeout(function () {
        fn.apply(this_, args);
        // 每隔wait时间将time置为空
        time = null;
      }, wait);
      if (action) {
        fn.apply(this_, args);
      };
    } else {
      // 不再是直接执行fn,在内部传递参数
      time = setTimeout(function () {
        // 通过apply修改fn的this
        fn.apply(this_, args);
      }, wait);
    }
  }
};

//节流
window.addEventListener('scroll', throttle(scrollFn, 1000));        
function scrollFn() {            
    console.log(new Date());        
};        
function throttle(fn, interval) {            
    var timer;            
    return function() {               
    if (timer) {                    
        return false;               
    }                
    timer = setTimeout(function() {                    
        console.log("throttle---");                    
        fn.apply();                    
        clearTimeout(timer);                    
        timer = null;                
    }, interval)}        
}  
console 命令行工具 X clear

                    
>
console