The heart beats - contracting and relaxing - due to electrical signals that are sent from the heart’s electrical system to the heart muscle.
Read about magnesium preventing cardiac arrest here -
https://drcarolyndean.com/2015/07/magnesium-prevents-cardiac-arrest/