Conditions for stable layers in Jupiter and Saturn over time