>In that case the while loop will help, but ultimately the number of repetitions will be wrong (as the check against the number of repetitions is after the while which may trigger the callback too often).

Ah, nice catch.