js防抖

function debounce(fn,delay){

  var time = null;

  return function(){

    var context = this;

    var args = argument;

    clearTimeout(time);

    time = setTimeout(function(){

      fn.apply(context, args);    

    },delay)

  }

}

// 当用户滚动时被调用的函数 function foo() { console.log('You are scrolling!'); } // 在 debounce 中包装我们的函数,过 2 秒触发一次 let elem = document.getElementById('container'); elem.addEventListener('scroll', debounce(foo, 2000));

posted @ 2018-03-15 11:06  Y-HJ  阅读(107)  评论(0编辑  收藏  举报