‘As’ has multiple meanings. It can be used for stuff like comparisons, but also has this meaning related to time.
In this context, it means “while this is happening”, so it gives a sense of continuation to the relevant action.
‘When’, on the other hand, tends to be more instantaneous.
In “when you doze off”, dozing off is conceptualized as a binary state. Resulting from the moment you fall asleep, something else then happens.
In “as you doze off”, dozing off is conceptualized as a gradual process, during which something else also gradually happens.