From mentalhelp.net
Constant Counting Disorder
This counting behavior may be nothing to speak of (clinical-wise), or possibly it could be an outward manifestation of an anxiety disorder, such as Obsessive-Compulsive Disorder (OCD), or possibly a neurological problem...
Counting things is a common behavioral theme, a sort of compulsion for some with OCD.
A compulsion is a behavior that is acted out in an attempt to quiet down the obsessive thoughts.
As such, it is typically an anxiety reducing behavior and that is why it is repeated so frequently. I’m not sure what obsession a counting compulsion might be compensating for... but the general pattern is that by acting out the behavior, people feel safer/better.