A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
一个零或更大的延迟时间,以毫秒为单位。对于事件回调,大约100或250(甚至更高)的值最为实用。
A function to be executed after delay milliseconds. The this context and all arguments are passed through, as-is, to callback when the throttled-function is executed.
一个在延迟毫秒后执行的函数。当节流函数执行时,this上下文和所有参数都会原样传递给callback。
Optionaloptions: ThrottleOptionsAn object to configure options. 用于配置选项的对象。
Throttle Options
OptionaldebounceMode?: booleanIf debounceMode is true (at begin), the callback is executed at the beginning
of the delay period. If debounceMode is false (at end), the callback is executed
at the end of the delay period.
如果 debounceMode 为 true(前缘模式),回调函数在延迟期开始时执行。
如果 debounceMode 为 false(后缘模式),回调函数在延迟期结束时执行。
OptionalnoLeading?: booleanOptional, defaults to false. If noLeading is false, the first throttled-function call will execute callback immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that callback will never executed if both noLeading = true and noTrailing = true.
可选,默认为false。如果noLeading为false,第一次节流函数调用将立即执行回调。 如果noLeading为true,第一次回调执行将被跳过。 需要注意的是,如果noLeading = true且noTrailing = true,回调将永远不会被执行。
OptionalnoTrailing?: booleanOptional, defaults to false. If noTrailing is true, callback will only execute
every delay milliseconds while the throttled-function is being called. If
noTrailing is false or unspecified, callback will be executed one final time
after the last throttled-function call. (After the throttled-function has not
been called for delay milliseconds, the internal counter is reset)
可选,默认为false。如果noTrailing为true,回调函数仅在节流函数被调用时每delay毫秒执行一次。
如果noTrailing为false或未指定,回调函数将在最后一次节流函数调用后额外执行一次。
(当节流函数在delay毫秒内未被调用时,内部计数器将被重置)
A throttled version of the callback with a .cancel() method. 带有 .cancel() 方法的节流版本回调函数。
The returned function includes a cancel(options?: CancelOptions) method:
cancel() — cancels all future executionscancel({ upcomingOnly: true }) — cancels only the next scheduled execution, but allows subsequent callsWhen both noLeading=true and noTrailing=true, the callback will never execute.
返回的函数包含 cancel(options?: CancelOptions) 方法:
cancel() — 取消所有未来执行cancel({ upcomingOnly: true }) — 仅取消下次计划执行,允许后续调用当 noLeading=true 和 noTrailing=true 同时设置时,回调永远不会执行。
Basic throttle — scroll handler that fires at most once every 100ms:
const onScroll = throttle(100, () => {
console.log('scrolled')
})
window.addEventListener('scroll', onScroll)
With noLeading — skip the first call, execute after 200ms of inactivity:
const fn = throttle(200, () => console.log('tick'), { noLeading: true })
fn() // skipped (noLeading)
fn() // scheduled for trailing edge
Debounce mode — execute at the beginning of the delay period:
const fn = throttle(300, () => console.log('debounced'), { debounceMode: true })
fn() // => 'debounced' (immediately)
fn() // ignored (within delay)
fn() // ignored (within delay)
// After 300ms of inactivity, next call triggers immediately again
Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.
限制函数的执行频率。特别适用于限制事件处理程序的执行速率,例如调整大小和滚动事件。