模拟实现throttle节流
throttle 节流
- 执行触发事件,每隔一段时间后,就触发一次真实函数
场景
- 浏览器 scroll 时,滚动时,隔断时间触发一次回调
实现
根据时间戳
- 触发 throttle 后,会立即执行一次函数
- 假设 wait 为 1 秒,那么在 1.2 秒时停止触发,则函数就不会再触发
function throttle(fun, wait) {
let time, prev = 0
return function() {
let context = this
let args = arguments
let now = +New Date()
if (now - prev > wait) {
fun.apply(context, args)
prev = now
}
}
}
使用定时器
- 触发 throttle 后,不会立即执行一次函数
- 假设 wait 为 1 秒,那么在 1.2 秒时停止触发,函数会在 2 秒时,再执行一次函数
function throttle() {
let time
return function () {
let context = this
let args = arguments
if (!time) {
time = setTimeout(function () {
time = null
fun.apply(context, args)
}, wait)
}
}
}