Interestingly, the heartbeat is not in fact perfectly periodic, but is really chaotic!
Since each cardiac cell on its own is a nonlinear oscillator capable of influencing and being influenced by its neighbours (in a mutual synchronization process called entrainment), the whole heart is essentially a distributed peer-to-peer network of coupled oscillators! This means that the overall quasi-periodic heartbeat produced by the complex interactions of individual cardiac cells is actually an emergent phenomenon that can be modelled using a strange attractor!
This also explains why the heart can be paced by a pacemaker from either the atrium or the ventricle; it doesn't matter at which end the stimulation occurs since the local cardiac cells will propagate it to their neigbours and so on, until they all eventually get entrained into their normal chaotic loop. This discovery was key in the development of the pacemaker, as atrial pacing proved to be impractical due to the required suturing that scarred the heart. Instead, a wire can simply be threaded down to the bottom of the heart in the ventricle, allowing it to naturally rest in a stable position.
Normally, the nervous system paces the heart from the atrium (by stimulating a bundle of nerves called the sinoatrial node), which must initially oscillate at a higher frequency than the rest of the heart in order to lead the heartbeat. Therefore, in order to pace the heart, a pacemaker must be stimulating it at the highest frequency, otherwise the heart will pace the pacemaker! (Obviously an undesirable situation.)
Of course, when the "communication lines" of the cardiac electrical network are
blocked for whatever reason, that's when you run into serious trouble (see
arrhythmia).
If you ever get the opportunity to take a course in cellular bioelectricity, I would highly recommend it!