Polling
If you need to trigger requests at regular intervals, which is common in large-screen data businesses, we have built-in polling options to easily solve this problem.
As shown below, you only need to set a pollingInterval
to tell us the polling interval time (in milliseconds).
Of course, this is just the beginning. We can use this feature to achieve even more interesting effects, such as syncing multiple tabs or even data between multiple devices.
In the following example, we use iframe to simulate multiple tabs in a browser. When you enter information in any of the tabs, the other tabs will synchronize the data.
You can also copy the link from the address bar above the example and try this feature in your own browser.
PollingWhenHidden
By default, we will pause polling when the screen is not visible and reactivate it when the screen is refocused. Of course, you can set pollingWhenHidden = true
to continue sending requests even when the screen is not visible.
import { useRequest } from 'vue-request';
const { data, loading } = useRequest(getTodo, {
pollingInterval: 1000,
pollingWhenHidden: true,
});
pollingWhenOffline
By default, we will pause polling when the network is disconnected and reactivate it when the network is restored. Of course, you can set pollingWhenOffline = true
to continue sending requests even when the network is disconnected.
Note
VueRequest checks whether the network is connected by listening to window.ononline
. In some cases, this event may be unreliable. For more information, please refer to MDNopen in new window.
import { useRequest } from 'vue-request';
const { data, loading } = useRequest(getTodo, {
pollingInterval: 1000,
pollingWhenOffline: true,
});