Raycast for a period time

Is there a way to raycast for a period of time? Like 4 seconds. I’ve been looking but I haven’t found a solution.

Have an Always cast the Ray, then have a Timer set to 40 (that is 4 seconds) turn a Switch off, preventing the Ray from turning on.