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.
Time in milliseconds.
A timer reference.
Functions
start/0
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.
apply_after/4
Evaluates apply(
after
amount of time
has elapsed. Returns {ok,
, or {error,
.
send_after/2
send_after/3
Evaluates Same as send_after/3
after
amount
of time has elapsed. (
can also be an atom of a
registered name.) Returns {ok,
, or
{error,
.send_after/2
send_after(
.
kill_after/1
kill_after/2
exit_after/2
exit_after/3
Send an exit signal with reason Same as Same as Same as exit_after/3
to Pid
. Returns {ok,
, or
{error,
.exit_after/2
exit_after(
. kill_after/2
exit_after(
. kill_after/1
exit_after(
.
apply_interval/4
Evaluates apply(
repeatedly at
intervals of
. Returns {ok,
, or
{error,
.
send_interval/2
send_interval/3
Evaluates Same as send_interval/3
repeatedly after
amount of time has elapsed. (
can also be an atom of
a registered name.) Returns {ok,
or
{error,
.send_interval/2
send_interval(
.
cancel/1
Cancels a previously requested timeout.
is a unique
timer reference returned by the timer function in question. Returns
{ok, cancel}
, or {error,
when
is not a timer reference.
sleep/1
Suspends the process calling this function for
amount
of milliseconds and then returns ok
, or suspend the process
forever if
is the atom infinity
. Naturally, this
function does not return immediately.
tc/1
tc/2
tc/3
Time = In microseconds
Evaluates Evaluates Evaluates tc/3
apply(
and measures
the elapsed real time as reported by os:timestamp/0
.
Returns {
, where
is the elapsed real time in microseconds,
and
is what is returned from the apply.tc/2
apply(
. Otherwise works
like tc/3
.tc/1
. Otherwise works like tc/2
.
now_diff/2
Tdiff = In microseconds
Calculates the time difference
in
microseconds, where
and
probably
are timestamp tuples returned from erlang:now/0
.
seconds/1
Returns the number of milliseconds in
.
minutes/1
Return the number of milliseconds in
.
hours/1
Returns the number of milliseconds in
.
hms/3
Returns the number of milliseconds in
.
Examples
This example illustrates how to print out "Hello World!" in 5 seconds:
1> timer:apply_after(5000, io, format, ["~nHello World!~n", []]).
{ok,TRef}
Hello World!
The following coding example illustrates a process which performs a certain action and if this action is not completed within a certain limit, then the process is killed.
Pid = spawn(mod, fun, [foo, bar]), %% If pid is not finished in 10 seconds, kill him {ok, R} = timer:kill_after(timer:seconds(10), Pid), ... %% We change our mind... timer:cancel(R), ...
WARNING
A timer can always be removed by calling cancel/1
.
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
.