let timer = null
// 防抖 => 在连续触发事件,在规定时间内只触发最后一次
// 应用场景 : 搜索框搜索请求数据
function timerFn() {
if (timer !== null) {
clearTimeout(timer)
}
timer = setTimeout(() => {
console.log("防抖")
}, 3000)
}
// 节流 => 在连续触发事件,在规定时间内只触发一次
// 应用场景 : 按钮的快速点击
const timerFN = function () {
if (timer !== null) {
return
}
timer = setTimeout(() =>{
console.log("节流")
timer = null
},1000)
}
10-28
250
09-03
1万+
05-29
1万+
07-16
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交