Detecting Third button press

I made it so that whenever I press button 3, I set a flag, stopLights to true. Then when i press button 2, I put a condition before executing beginCount(i+1); saying that if stopLights is true, then break out of the loop. However, the issue is that button 3 won't detect input until the for loop is done executing the code.