This is being called whenever the "play" button is clicked. So, it was automatically incrementing every time it was clicked, since it was doing the check for the position of the iteration after incrementing, instead of before. That said, it it probably better to avoid calling this altogether by doing a check on the play() function for whether or not the "play" button is disabled and/or if the current iteration is less than the maximum iteration.