轮询

通过设置 options.pollingInterval,进入轮询模式,useRequest 会定时触发 service 执行。

const { data, run, cancel } = useRequest(getUsername, {
  pollingInterval: 3000,
});

例如上面的场景,会每隔 3000ms 请求一次 getUsername。同时你可以通过 cancel 来停止轮询,通过 run/runAsync 来启动轮询。

你可以通过下面的示例来体验效果

- data:
点击查看代码
<script setup>
import { mockFetchFast, mockFetchError } from "@/api";
import { useRequest } from "ahooks-for-vue";

const { loading, data, cancel, run } = useRequest(mockFetchFast, {
  pollingInterval: 3000,
  // defaultParams: { },
  // pollingErrorRetryCount: 10,
  // pollingWhenHidden: false,
});
const set = (v) => {
  v ? run() : cancel();
};
</script>
<template>
  <div>
    <button @click="() => set(true)">start</button>
    -
    <button @click="() => set(false)">stop</button>
    data: {{ loading ? "loading..." : data }}
  </div>
</template>
<style></style>

轮询错误重试

通过 options.pollingErrorRetryCount 轮询错误重试次数。

const { data, run, cancel } = useRequest(getUsername, {
  pollingInterval: 3000,
  pollingErrorRetryCount: 3,
});

你可以通过下面的示例来体验效果。

API

Return

参数说明类型
run启动轮询(...params: TParams) => void
runAsync启动轮询(...params: TParams) => Promise<TData>
cancel停止轮询() => void

Options

参数说明类型默认值
pollingInterval轮询间隔,单位为毫秒。如果值大于 0,则处于轮询模式。Ref<number> | number0
pollingWhenHidden在页面隐藏时,是否继续轮询。如果设置为 false,在页面隐藏时会暂时停止轮询,页面重新显示时继续上次轮询。Ref<boolean> | booleantrue
pollingErrorRetryCount轮询错误重试次数。如果设置为 -1,则无限次Ref<number> | number-1

备注

  • options.pollingIntervaloptions.pollingWhenHidden 支持动态变化。
  • 如果设置 options.manual = true,则初始化不会启动轮询,需要通过 run/runAsync 触发开始。
  • 如果设置 pollingInterval0 变成 大于 0 的值,不会启动轮询,需要通过 run/runAsync 触发开始。
  • 轮询原理是在每次请求完成后,等待 pollingInterval 时间,发起下一次请求。
Last Updated:
Contributors: 郑磊