Quantum interference and collapse of the wavefunction in cavity QED