You should only change on the fly when your team is on the attack. Don’t skate to the bench for a line change if the puck is in your defensive zone. “On the attack” means that your team is safely in possession of the puck, or that your team just dumped the puck into the offensive zone. Regardless, never change when the opposing team has the puck. This is the most important part of any line change.
Get off the ice if you get an opportunity to make a safe change, and you don’t think you have the energy to continue back-checking for 15 seconds. When in doubt, step off the ice.
If you have been on the ice for a minute it is time to start thinking about a change.