2019-03-09/函数节流与防抖

1.函数节流代码

var cd =false
function kill(){
  console.log("释放技能")
}
var element = document.getElementById('1')
element.onclick=function(){
if(cd){
  //什么也不做
}else{
      kill()
      cd=true
      setTimeout(function(
      ){
        cd = false
      },5000)
  }
}

2.函数防抖

var timer =null
function takeOut(){
  console.log('送外卖啦')
}

var ele = document.getElementById('2')
ele.onclick=function(){
 if(timer){
   window.clearTimeout(timer)
 }
 timer=setTimeout(function(){
   takeOut()
   timer=null
 },5000)
}

你可能感兴趣的:(2019-03-09/函数节流与防抖)