As @null and @prabushitha suggested, it will be not a good thing to use a loop for this (as it seems like). If you want to do something in a loop and change it or stop it by a user event, i’m afraid you have to use the timeout (while keeping the performance at a max level).
If you are okay to use timeout, these answers will help.
StackOverFlow - Start and Stop loop in JS using user event
P.S. :- If we can see the code, it will be a great help to give an answer