The need for independence was growing for a long time in Latin America, but until then the Spanish forces were able to block those movements for independence.
But when Napoleon invaded Spain, the rebels saw the chance they needed. When Napoleon invaded Spain he put his brother on the Spanish throne. This was the perfect timing for secession. Spain was not able to hold its rich colonies because most of the independence movement took place at the same time.