In my Angular 7 application, I am utilizing RxJS to handle asynchronous operations.
My goal is to retrieve a list of items from an API endpoint every 30 seconds. However, there are times when the request may take longer than expected, and I want to ensure that the next call accounts for this delay.
For example, if the initial request takes 10 seconds to complete, I wish to make the subsequent request after 40 seconds (30 + 10), instead of just 20 seconds (30 - 10).
I have attempted to achieve this using the following code:
fetchList() {
this.service.fetchListFromHttp()
.pipe(
finalize(() =>
setTimeout(() => {
this.fetchList();
}, 30000)
)
)
.subscribe(
result => this.list = result,
err => this.logError(err)
);
}
However, I have noticed strange behavior where the function gets called at irregular intervals like every 10 seconds or 20 seconds, rather than consistently every 30 seconds as anticipated. I expect the interval between calls to always be greater than 30 seconds.