some people do that in order to prevent a bad thought from occurring in real life like a car crash in their minds flipping the light switch on and off a certain amount of times prevented the car crash  or they might open and close the refrigerator a certain number of times in order to prevent some other unrelated disaster the feeling goes away after the repeated action is performed exactly right