Check timeout


I am making a kind of online game, and in this game there will be some phases. In these phases will have a timer and when the timer reach 0 the phase will change.

Now my question is how to make this timer.

The game will have a modal with the start time of the new phase and the current phase ( and many other fields)

One idea is to use BackgroundScheduler. Make a job every one second, check the models “game”, get the playing games and check if the timer is out. But I think that is not the best way.

A better way may be to create just a job when the timer finish?

Any other idea, library or method to this more efficient?


The way I approached this was using a worker task in Channels. When I needed to start a timer, I’d pass a message to the worker with an ID (this ID is unique per call, not per instance) and duration.

If everything was done before the timer event fires, I set a “complete” flag, and send a message to cancel the timer. (The ID lets me determine which timer gets cancelled.)

When the timer expires, it checks to see if the “complete” flag is set (that’s to try and catch the race condition where the last task was complete before the timer expires, but the timer expires before it gets notification of the completion.) If the “complete” flag isn’t set, then I set the “refuse all actions” flag and trigger the action to be performed when the tasks aren’t completed.

There are other race conditions that exist, but I do my best to do “defensive programming” to handle them safely.


1 Like


Thanks for your answer.

In my current project I have implemented channels so, these solutions fits perfect.

The main problem is that I can’t get how to implement the timer on a channel worker, I have done a little search and I find this:

This is what you mean?

Another question is that I could use Timer just on the view when I launch the game. This would be more simple, the only problem is that Timer will be on the ram, so if the server rebot may lose some timer.


Yes, that example expresses the basic ideas.

Using a timer in your view isn’t going to do you any good because the instance of your view goes out of scope when the view ends. It’s not valid to expect anything from your view to execute after the view has exited.