Why do we have a threefold repetition rule in chess?

This rule is to ensure the game ends and does not continue indefinitely.