节流
通过设置 options.throttleWait,进入节流模式,此时如果频繁触发 run 或者 runAsync,则会以节流策略进行请求。
const { data, run } = useRequest(getUsername, {
throttleWait: 300,
manual: true,
});
如上示例代码,频繁触发 run,只会每隔 300ms 执行一次。
你可以在下面 input 框中快速输入文本,体验效果
API
Options
throttle 所有参数用法和效果同 lodash.throttle
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| throttleWait | 节流等待时间, 单位为毫秒,设置后,进入节流模式 | number | - |
| throttleLeading | 在节流开始前执行调用 | boolean | true |
| throttleTrailing | 在节流结束后执行调用 | boolean | true |
备注
options.throttleWait、options.throttleLeading、options.throttleTrailing支持动态变化。runAsync在真正执行时,会返回Promise。在未被执行时,不会有任何返回。cancel可以中止正在等待执行的函数。