J-M-L
5
you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction
Otherwise there are many discussions about managing time and non blocking code - for extra information and examples look at