Is this a single thread/process?
If so, usually you take a timestamp before the loop, then at the bottom of the loop you check the time until you either
1. complete all the iterations anyway
2. run out of time and exit the loop.
Alternately, you can run multiple copies of a prog simultaneously using something like
nohup myprog &
inside a loop
Really need more info, there are many ways of multi-tasking.