节流
节流
当持续触发事件时,保证一定时间段内只调用一次事件处理函数。
let btn = document.querySelector('button')
function ajax(param) {
console.log(param);
}
btn.addEventListener('click',() => {
throttleAjax('节流!')
})
function Throttle(cb,wait) {
let flag = true
let that = this
return function(param){
if(flag) {
flag = false
setTimeout(() => {
cb.call(that,param)
flag = true
},wait)
}
}
}
let throttleAjax = Throttle(ajax,1000)