Periodically check on some models

Hi all, I have a little app which consists of a list of Todo models. Each Todo has a due timestamp field.

While the app runs, I want to use a single setTimeout and execute an action each minute. If a todo item is due, show an alert.

Something like this:

function checkModels () {
  // ... do some work using models
}

function tick () {
  checkModels()
  setTimeout(tick, 1000 * 60)
}

tick()

That will call the tick function every minute, and then do some work, which is what I want. What I’m not sure about is where to put that code.

Right now I have a simple route, which displays all Todos using a <TodoList /> component. Should the ticking be a service? Should it be in a controller? In a component?

If you want this behavior on just on one page I’d put it in a component (or controller if you want), otherwise I’d put it in a service. Then you could let the service run on app boot or whenever you enter/exit certain routes (that part is up to you). You could always put it in a component and extract it easily into a service later, but it’s also easy to do now so :person_shrugging: why not?

1 Like