Of course this means that you're actually not doing the exact same thing at the same company all the time. A good company allows for that. Either because you can easily switch projects, switch teams, move up, move sideways etc. Depends on things like company size, structure, culture etc. And yourself obviously.
I found that the times in my career where I learned the most was when I hung around to face the consequences of my actions. That normally means staying longer than two years.
Of course this means that you're actually not doing the exact same thing at the same company all the time. A good company allows for that. Either because you can easily switch projects, switch teams, move up, move sideways etc. Depends on things like company size, structure, culture etc. And yourself obviously.