美文网首页
2019-03-09/函数节流与防抖

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

作者: 阿九_beta | 来源:发表于2019-03-11 12:17 被阅读0次

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/函数节流与防抖

      本文链接:https://www.haomeiwen.com/subject/uvdspqtx.html