When answers the question "at what time?" It can convey the time of an actual event in the future or the past, or a nonspecific time that might occur. It can be used at the beginning of a sentence, or in the middle.
Ex: I'll do the dishes when I get home, since I know that there's quite a mess from last night's dinner that still needs cleaning.
Ex: It was snowing when I got to school yesterday, and we all sat around talking about whether we would be sent home early or not.
Ex: Matt knew that the fence needed to be fixed, so he said he would do it when he got a chance, and I believe him.
Before and After
Before and after convey the order of events in time, whether earlier or later. They are crucial for establishing a sequence of events, and are very important in directions of any kind. Both can be used at the beginning or in the middle of a sentence and the same comma rule of when applies here as well.
Ex: Before cleaning out the vacuum cleaner, it is very important to make sure that it is unplugged.
There is no reason to risk an electrical shock.
Ex: After she gave birth to her first daughter, Alice decided that she wouldn't go back to work straight
away. She stayed at home with her baby for almost a year before she returned to work.
Ex: Jennifer went home and did her maths homework, English homework, and science homework. After
that, she had dinner with her family.