timer

Timer Functions

This module provides useful functions related to time. Unless otherwise
stated, time is always measured in milliseconds. All
timer functions return immediately, regardless of work carried
out by another process.

Successful evaluations of the timer functions yield return values
containing a timer reference, denoted TRef below. By using
cancel/1, the returned reference can be used to cancel any
requested action. A TRef is an Erlang term, the contents
of which must not be altered.

The timeouts are not exact, but should be at least as long
as requested.

Functions

start() -> ok

Starts the timer server. Normally, the server does not need
to be started explicitly. It is started dynamically if it
is needed. This is useful during development, but in a
target system the server should be started explicitly. Use
configuration parameters for kernel for this.

send_interval(Time, Pid, Message) -> {ok, TRef} | {error, Reason}

send_interval(Time, Message) -> {ok, TRef} | {error, Reason}

Evaluates Pid ! Message repeatedly after Time
amount of time has elapsed. (Pid can also be an atom of
a registered name.) Returns {ok, TRef} or
{error, Reason}.

send_interval/2

Same as send_interval(Time, self(), Message).

cancel(TRef) -> {ok, cancel} | {error, Reason}

Cancels a previously requested timeout. TRef is a unique
timer reference returned by the timer function in question. Returns
{ok, cancel}, or {error, Reason} when TRef
is not a timer reference.

sleep(Time) -> ok

Time = integer() in milliseconds or the atom infinity

Suspends the process calling this function for Time amount
of milliseconds and then returns ok, or supend the process
forever if Time is the atom infinity. Naturally, this
function does not return immediately.

tc(Module, Function, Arguments) -> {Time, Value}

Module = Function = atom()

Arguments = [term()]

Time = integer() in microseconds

Value = term()

Evaluates apply(Module, Function, Arguments) and measures
the elapsed real time. Returns {Time, Value}, where
Time is the elapsed real time in microseconds,
and Value is what is returned from the apply.

now_diff(T2, T1) -> Tdiff

T1 = T2 = {MegaSecs, Secs, MicroSecs}

Tdiff = MegaSecs = Secs = MicroSecs = integer()

Calculates the time difference Tdiff = T2 - T1 in
microseconds, where T1 and T2 probably
are timestamp tuples returned from erlang:now/0.

seconds(Seconds) -> Milliseconds

Returns the number of milliseconds in Seconds.

minutes(Minutes) -> Milliseconds

Return the number of milliseconds in Minutes.

hours(Hours) -> Milliseconds

Returns the number of milliseconds in Hours.

hms(Hours, Minutes, Seconds) -> Milliseconds

Returns the number of milliseconds in Hours + Minutes + Seconds.

Examples

This example illustrates how to print out "Hello World!" in 5 seconds:

WARNING

An interval timer, i.e. a timer created by evaluating any of the
functions apply_interval/4, send_interval/3, and
send_interval/2, is linked to the process towards which
the timer performs its task.

A one-shot timer, i.e. a timer created by evaluating any of the
functions apply_after/4, send_after/3,
send_after/2, exit_after/3, exit_after/2,
kill_after/2, and kill_after/1 is not linked to any
process. Hence, such a timer is removed only when it reaches its
timeout, or if it is explicitly removed by a call to cancel/1.