defmodule Elevator.Timer do def keepalive_timer(parent, id, msecs) do receive do :keepalive -> keepalive_timer(parent, id, msecs) after msecs -> send(parent, {:timeout, id}) end end end #To run: # this_pid = self # spawn fn -> Elevator.Timer.keepalive_timer(this_pid, 1, 5000) end