// 常用于window resize 或 scroll监听
function debounce(fn, wait) {
wait = wait || 0
let timerId
return function () {
let context = this
let args = arguments
if (timerId) {
clearTimeout(timerId)
timerId = null
}
timerId = setTimeout(function () {
fn.apply(context, args)
}, wait)
}
}
// 常用于按钮提交 或 实时查询
function throttle(fn, threshhold) {
let last, timer
threshhold || (threshhold = 200)
return function () {
let context = this
let args = arguments
let now = +new Date()
if ((last && now < last + threshhold)) {
clearTimeout(timer)
timer = setTimeout(function () {
last = now
fn.apply(context, args)
}, threshhold)
} else {
last = now
fn.apply(context, args)
}
}
}
网友评论