节流防抖

防抖的应用

 

 

function debounce(fn,delay) {
  let timer
  return function() {
    clearTimeout(timer)
 var args = arguments
    timer = setTimeout(function() {
      fn(args)
    },delay)
  }
}
function a(value) {
  console.log('输出', value)
}
const input = document.getElementById('ipt')
const debounceInput = debounce(a,1000)
input.addEventListener('keyup',function(e) {
  debounceInput(e.target.value)
})
 
 
 

 

 

function throttle(fn,delay){
  let timer
  return function() {
 var args = arguments
    if(!timer){
      timer = setTimeout(function(){
        fn(args)
   clearTimeout(timer)
        timer = null
      },delay)
    }
  }
}

 

 

 

 

 

 

 

posted @ 2020-10-28 14:45  super_素素  阅读(59)  评论(0编辑  收藏  举报