obvously you never looked at the debug console to see what is happening with your esp.
or even put some serial output to see where you are or stay in your sketch.
you better rewrite your sketch in a way that it keeps looping, branching on some increasing counter and just executing the "other" branch when the threshold is exceeded.