每天一个前端小知识06——节流、防抖

节流(throttle)

单位时间内频繁触发只执行一次,类似于技能冷却

  • 高频事件
  • 下拉加载

let timer = null
document.querySelector(‘.ipt’).onkeyup = function() {
if(timer !== null) {
return
}
timer = setTimeout(() => {
console.log(‘节流’)
timer = null
},1000)
}

防抖(debounce)

单位时间内频繁触发只执行最后一次,,类似于游戏回城

  • 搜索框搜索输入
  • 文本编辑器实时保存

let timer = null
document.querySelector(‘.ipt’).onkeyup = function() {
if(timer !== null) {
clearTimeout(timer)
}
timer = setTimeout(() => {
console.log(‘防抖’)
},1000)
}

猜你喜欢

转载自blog.csdn.net/qq_33591873/article/details/128225940