proverb

Get out while the going is good.

leave

suggests that

You should leave, escape, or extricate from some place or situation while you have the chance, before conditions worsen and becomes impossible, or too dangerous to do so.