The vibration from the mirror, shutter, and aperture diaphragm might be assisting gravity in pulling the lens' focusing elements out of focus. If so, the point of focus should drift closer to the camera when pointed down and further from the camera when pointed upward. As the focusing elements of the lens extend, the point of focus gets shorter. As the focusing elements retract, the point of focus moves towards infinity.
This is not uncommon with the zoom elements of zoom lenses or the focusing elements of lenses with heavier glass. It's a little less expected with a lightweight lens such as the EF-S 24mm f/2.8 STM. Further complicating things is the fact that this is an STM lens.
With conventional manual focus only, micro-motor, or USM lenses one popular solution is to immobilize the focusing ring using tape or a large elastic band in the same way one would immobilize a zoom ring to prevent zoom creep. The direct mechanical link between the focusing ring and the focusing elements prevents, or, at the very least, minimizes, movement of the lens elements.
But with STM lenses there is no direct connection between the focusing ring and the focus elements in the lens. It's all focus-by-wire. When the focusing ring is moved the movement is measured by an electric sensor, a signal is sent to the camera body which then sends a signal to the lens to move the focusing elements using the same stepper motor that the AF system uses to move the lens. The only time moving the focusing ring of STM lenses does anything is when metering is active. If the camera is turned off, on standby, or metering has timed out then turning the manual focus ring does nothing. Presumably this means the AF motor is only energized when metering is active.
If your timelapse is set up so that the interval between each shot is longer than your camera's metering timeout, then there are periods between each shot when the AF motor is not energized.
Based on the description of your experience, it also seems that when AF is not activated via the shutter button the AF motor may not be actively engaged to hold a position even when metering is active. This essentially leaves the lens elements that move to adjust focus free to drift.
Without having a copy of the EF-S 24mm f/2.8 STM to examine and experiment on, it's hard to say what the best approach would be to prevent movement of the lens elements during a timelapse. If the part of the lens that extends and retracts as the focus distance is changed is out far enough, you might try to fix it in place with gaffer's tape. If your focus distance is such that the center part of the front group is not extended, then finding a way to fix the focus position may be more difficult with that particular lens.
The optional lens hood for the EF-S 24mm STM (The ES-52 for the EF 40mm f/2.8 STM 'Pancake' discussed in this question works similarly) would give you an extension of the inner barrel that contains the front elements. Presumably so would anything else that could attach to the threads for the hood that don't interfere with the optical path.