Break from while loops after specific time

Yes, you are on the right track. Take a timestamp just before the loop starts and compare it against millis().

But it would be best to try and address this issue: