# Potential race condition with timers

**URL:** <https://forum.kirupa.com/t/potential-race-condition-with-timers/246239>\
**Category:** flash\
**Created:** [December 8, 2007, 6:49pm UTC](https://forum.kirupa.com/t/potential-race-condition-with-timers/246239 "2007-12-08T18:49:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![raal](https://avatars.discourse-cdn.com/v4/letter/r/a4c791/32.png) [@raal](https://forum.kirupa.com/u/raal)\
**Post date:** [December 8, 2007, 6:49pm UTC](https://forum.kirupa.com/t/potential-race-condition-with-timers/246239/1 "2007-12-08T18:49:57Z")

</div>

Allow me to give an example:

myTimer1 :Timer = new Timer(500);  
myTimer2 :Timer = new Timer(1000);

myTimer1.addEventListener(“timer”, TimerFunctionOne);  
myTimer2.addEventListener(“timer2”,TimerFunctionTwo);

MyTimer1.start();

//------------------------  
function TimerFunctionOne()  
{  
//do some operation here  
myTimer1.stop();  
myTimer2.start();  
}

## function TimerFunctionTwo() { //do some operation here myTimer2.stop(); myTimer1.start(); }

Now for the tricky bit. Suppose you want this in a class, and you want the ability to pause the class. I can add states to the class which describe what it is doing at the moment, and then stop the relevant timers. However, I can’t seem to find a way to avoid the race condition where the pause function on the object is called just as one of the functions have been called through the timer, and then starts a timer again.

I can add an “if (!paused)” to the timer starters in TimerFunctionOne/Two, but that still leaves a theoretical hole (although small).

So, any thoughts on this?

raal
