Whenever looking at animations in VMD that have a selection string using "within" or a function of atom positions, then one needs to update the selection, or else VMD will show the atoms that would satisfy the selection condition at the frame it was defined only. There is an option to automatically "Update Selection [in] Every Frame" in the GUI, but that can lead to distractions, because at the boundary where atoms are moving in and out of the selection, any animation will look very busy and may needlessly call attention to it. Better would be to have this update of the selection happen only every N steps and even better would be, if this N would have some degree of randomness to it. The following little Tcl script realizes just that.
This subroutine needs some (global) defaults and settings, so they can be passed to the callback function.
Final step is to add the function to the list of callbacks for the VMD animation loop for this molecule.