I am creating an Angular application that will poll a backend API at regular intervals. I would like to keep the data on the page up-to-date with the information on the backend, in case it changes independently of a user's action in the front end.
When the user completes an action on the page, it will kick off the polling. Initially, the action returns an object with the new state of the item. However, soon after completing that action, the object's status may change on the backend. Over time, the chance that the object's status will change without user interaction drops.
I would like to implement polling that uses an increasing timeout for each subsequent call, to model the expected behavior of the object and reduce strain on the backend resource.
For example:
- Initial call, wait 1 second
- Repeat call, wait 2 seconds
- Repeat call, wait 4 seconds
- Repeat call, wait 8 seconds
- Repeat call, wait 16 seconds
I have seen articles that describe how to set up polling with Observables in RxJS, but they all seem to use a static timeout (i.e. always wait 5 seconds between calls).
How can I repeat an HTTP call within Angular at regular, increasing intervals?