How to set a temporized task?

In my app I have to do some operations with some frequency without user intervention. To do that I need a timer which launches a task again and again.

For example, I have to show warnings about the state related to business rules. This warning come from calculations made daily over the data stored in the app.

What is the better approach to set a temporized task like in a timer clock?

You can always use good ol’ setInterval:

setInterval(() => {
console.log(‘Business business business! (Is this working?)’);
}, 1000);

Of course, this assumes that your app will be running in the foreground.

If the app was force stopped and the timeout was in the period when it was stopped what happens when the app is open again?

rxjs’s schedulePeriodic looks like another possibility.

setInterval() alone may cause you trouble if any changes you make inside it have to be propagated to any of your views, because of the way zone.js and angular2 work together - e.g. this article -

and RxJS schedulePeriodic() is mostly just setInterval() with some bells and whistles added, so it still does not solve your problem. You must take zone into account.

I wrote an injectable provider class that’s pretty short and self contained to do exactly what you’re asking for. Been using it for a couple of months with no problems. It is here.