shadcn-ahooks
useRequest

Throttle

The API useRequest

Throttle

Enter the throttle mode by setting options.throttleWait. At this time, if run or runAsync is triggered frequently, the request will be executed with the throttle strategy.

const { data, run } = useRequest(getUsername, {
  throttleWait: 300,
  manual: true
});

As in the example code above, if run is triggered frequently, it will only be executed once every 300ms.

You can quickly enter text in the input box below to experience the effect

API

Options

The usage and effects of all throttle property are the same as lodash.throttle

PropertyDescriptionTypeDefault Value
throttleWaitThrottle wait time, in milliseconds. After setting, enter the throttle modenumber-
throttleLeadingExecute the request before throttling startsbooleantrue
throttleTrailingExecute the request after throttling endsbooleantrue

Remark

  • options.throttleWait, options.throttleLeading, options.throttleTrailing support dynamic changes.
  • runAsync will return a Promise when it is actually executed. When it is not executed, there will be no return.
  • cancel can abort a function waiting to be executed.

On this page