Skip to content

Instantly share code, notes, and snippets.

@devonestes
Last active June 13, 2018 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devonestes/3c17565f39f21966a9fa5352b991efbc to your computer and use it in GitHub Desktop.
Save devonestes/3c17565f39f21966a9fa5352b991efbc to your computer and use it in GitHub Desktop.
defmodule SendAfter do
def send_after(pid, message, nanoseconds) do
{_, os_name} = :os.type()
increment =
if os_name == :Windows do
# monotonic time for Windows in miliseconds, in Linux and MacOS it's nanoseconds
# so we'll need to convert to miliseconds for windows
nanoseconds / 1_000_000
else
nanoseconds
end
start_time = :erlang.monotonic_time()
end_time = start_time + increment
Task.async(fn -> wait_and_send(end_time, pid, message) end)
end
def wait_and_send(end_time, pid, message) do
if :erlang.monotonic_time() >= end_time do
send(pid, message)
else
wait_and_send(end_time, pid, message)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment