useThrottleFn
限制函数的执行。特别适用于在调整大小和滚动等事件上限制处理程序执行速率。
Throttle是一个投球的弹簧:当球飞出后,它需要一些时间来收缩,所以除非准备好,否则它不能再投球。
用法
js
import { useThrottleFn } from '@mpxjs/mpxuse-core'
const throttledFn = useThrottleFn(() => {
// do something, it will be called at most 1 time per second
}, 1000)
const onScrolling = () => throttledFn()
import { useThrottleFn } from '@mpxjs/mpxuse-core'
const throttledFn = useThrottleFn(() => {
// do something, it will be called at most 1 time per second
}, 1000)
const onScrolling = () => throttledFn()
推荐阅读
类型声明
typescript
/**
* 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.
*
* @param [trailing=false] if true, call fn again after the time is up
*
* @param [leading=true] if true, call fn on the leading edge of the ms timeout
*
* @return A new, throttled, function.
*/
export declare function useThrottleFn<T extends FunctionArgs>(
fn: T,
ms?: MaybeComputedRef<number>,
trailing?: boolean,
leading?: boolean
): T
/**
* 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.
*
* @param [trailing=false] if true, call fn again after the time is up
*
* @param [leading=true] if true, call fn on the leading edge of the ms timeout
*
* @return A new, throttled, function.
*/
export declare function useThrottleFn<T extends FunctionArgs>(
fn: T,
ms?: MaybeComputedRef<number>,
trailing?: boolean,
leading?: boolean
): T