Elements with an [up-poll]
attribute are reloaded from the server periodically.
Assume an application layout with an unread message counter.
You can use [up-poll]
to refresh the counter every 30 seconds:
<div class="unread-count" up-poll>
2 new messages
</div>
You may set an optional [up-interval]
attribute to set the reload interval in milliseconds:
<div class="unread-count" up-poll up-interval="10000">
2 new messages
</div>
If the value is omitted, a global default is used. You may configure the default like this:
up.radio.config.pollInterval = 10000
The element will be reloaded from the URL from which it was originally loaded.
To reload from another URL, set an [up-source]
attribute on the polling element:
<div class="unread-count" up-poll up-source="/unread-count">
2 new messages
</div>
A target selector will be derived from the polling element.
By default polling will pause while the fragment's layer is covered by an overlay.
When the layer is uncovered, polling will resume.
To keep polling on background layers, set [up-if-layer=any]
.
Polling will also pause automatically while the browser tab is hidden. When the browser tab is re-activated, polling will resume.
When at least one poll interval was spent paused in the background and the user then returns to the layer or tab, Unpoly will immediately reload the fragment. You can use this to load recent data when the user returns to your app after working on something else for a while. For example, the following would reload your main element after an absence of 5 minutes or more:
<main up-poll up-interval="300_000">
...
</main>
Client-side code may skip an update by preventing an up:fragment:poll
event
on the polling fragment.
When polling a fragment periodically we want to avoid rendering unchanged content. This saves CPU time and reduces the bandwidth cost for a request/response exchange to about 1 KB (1 packet).
See Skipping rendering for more details and examples.
When an update is skipped, Unpoly will try to poll again after the configured interval.
There are multiple ways to stop the polling interval:
[up-poll]
attribute.[up-poll="false"]
attribute.up.radio.stopPolling()
with the polling element.The reload interval in milliseconds.
Defaults to up.radio.config.pollInterval
, which defaults to 30 seconds.
The URL from which to reload the fragment.
Defaults to the URL this fragment was originally loaded from.
The HTTP method used to reload the fragment.
A JSON object with additional request headers.
A JSON object with additional parameters that should be sent as the request's query string or payload.
When making a GET
request to a URL with a query string, the given { params }
will be added
to the query parameters.
Whether to preserve the polling fragment's data object through reloads.
Controls polling while the fragment's layer is covered by an overlay.
When set to 'front'
, polling will pause while the fragment's layer is covered by an overlay.
When the fragment's layer is uncovered, polling will resume.
When set to 'any'
, polling will continue on background layers.