Once in the loop you're not checking the current state of the button, only the state you saved in the variable. Change the code to have it check the current state inside the loop.