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

2019-03-11  本文已影响0人  阿九_beta

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)
}
上一篇 下一篇

猜你喜欢

热点阅读