- 防抖函数
function debounce (func, delay = 200) {
let timer = null
return (...params) => {
timer && clearTimeout(timer)
timer = setTimeout(() => func(...params), delay)
}
}
- 节流函数
function throttle (func, delay = 200) {
let timer = null
return (...params) => {
if (!timer) {
timer = setTimeout(() => {
func(...params)
timer = null
}, delay)
}
}
}
评论
还没有任何评论,你来说两句吧!