The captureFrame() function sets the time back to 0 when it is done. Please don't do it here.  All you need to do here is ignore the event.  Just add a test for the global flag to the if statement above.