How to achieve a repeat function that can be stopped

Does that compile?