I made a simple version in a JSFiddle of what you may be interested in, and hopefully extract the components that will be helpful to you. Also threw in a ton of comments to help understand the pieces.
The basic idea behind my method is having the setInterval()
act as your for loop, and once per loop it will check to see if clearInterval()
has been called on it. You will have a global counter to keep track of the position.