Polling
If you need to regularly trigger a request, we have built-in a polling option, which can quickly help you solve this matter.
As shown below, you only need to set a pollingInterval
to tell us the polling interval (milliseconds)
Of course, this is just a starter. We can use this feature to achieve some more fun effects, such as synchronizing multiple tabs, or even data between multiple devices.
In the demo below, we use iframe to simulate multiple tabs of the browser. When a user makes a change, both tabs will eventually render the same data.
You can copy the link in the address bar above the demo and try this in your browser.
PollingWhenHidden
By default, we will pause polling when the screen is not visible. When the screen is refocused, polling is reactivated. Of course, you can set pollingWhenHidden = true
to continue making requests even when the screen is invisible.
import { useRequest } from 'vue-request';
const { data, loading } = useRequest('api/todo', {
pollingInterval: 1000,
pollingWhenHidden: true,
});
pollingWhenOffline
By default, we will pause polling when the network is disconnected. When the network reconnect to normal, reactivate polling. Of course, you can set pollingWhenOffline = true
to continue making requests even when the network is disconnected.
Note
VueRequest checks whether the network is online by listening to window.ononline
. In some cases, this event may be unreliable. See also MDNopen in new window
import { useRequest } from 'vue-request';
const { data, loading } = useRequest('api/todo', {
pollingInterval: 1000,
pollingWhenOffline: true,
});