Skip to content

⬅️ Back to Table of Contents

📄 useThrottleFn

📊 Analysis Summary

Metric Count
🔧 Functions 1
📦 Imports 5

📚 Table of Contents

🛠️ File Location:

📂 packages/shared/useThrottleFn/index.ts

📦 Imports

Name Source
MaybeRefOrGetter vue
FunctionArgs ../utils
PromisifyFn ../utils
createFilterWrapper ../utils
throttleFilter ../utils

Functions

useThrottleFn(fn: T, ms: MaybeRefOrGetter<number>, trailing: boolean, leading: boolean, rejectOnCancel: boolean): PromisifyFn<T>

Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.

Parameters:

  • fn any: 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.
  • ms any: A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful. (default value: 200)
  • trailing any (optional): if true, call fn again after the time is up (default value: true)
  • leading any (optional): if true, call fn on the leading edge of the ms timeout (default value: true)
  • rejectOnCancel any (optional): if true, reject the last call if it's been cancel (default value: false)

Returns: undefined A new, throttled, function.

Tags: @__NO_SIDE_EFFECTS__

Raw JSDoc
/**
 * Throttle execution of a function. Especially useful for rate limiting
 * execution of handlers on events like resize and scroll.
 *
 * @param   fn             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.
 * @param   ms             A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
 *                                    (default value: 200)
 *
 * @param [trailing] if true, call fn again after the time is up (default value: true)
 *
 * @param [leading] if true, call fn on the leading edge of the ms timeout (default value: true)
 *
 * @param [rejectOnCancel] if true, reject the last call if it's been cancel (default value: false)
 *
 * @return  A new, throttled, function.
 *
 * @__NO_SIDE_EFFECTS__
 */

Calls:

  • createFilterWrapper (from ../utils)
  • throttleFilter (from ../utils)
Code
export function useThrottleFn<T extends FunctionArgs>(
  fn: T,
  ms: MaybeRefOrGetter<number> = 200,
  trailing = true,
  leading = true,
  rejectOnCancel = false,
): PromisifyFn<T> {
  return createFilterWrapper(
    throttleFilter(ms, trailing, leading, rejectOnCancel),
    fn,
  )
}

Generated by Syntax Scribe