1timer(3)                   Erlang Module Definition                   timer(3)
2
3
4

NAME

6       timer - Timer functions.
7

DESCRIPTION

9       This module provides useful functions related to time. Unless otherwise
10       stated, time is always measured in milliseconds.  All  timer  functions
11       return immediately, regardless of work done by another process.
12
13       Successful  evaluations  of the timer functions give return values con‐
14       taining a timer reference, denoted TRef. By  using  cancel/1,  the  re‐
15       turned  reference can be used to cancel any requested action. A TRef is
16       an Erlang term, which contents must not be changed.
17
18       The time-outs are not exact, but are at least as long as requested.
19
20       Creating timers using erlang:send_after/3 and  erlang:start_timer/3  is
21       more  efficient than using the timers provided by this module. However,
22       the timer module has been improved in OTP 25, making it more  efficient
23       and  less susceptible to being overloaded. See the Timer Module section
24       in the Efficiency Guide.
25

DATA TYPES

27       time() = integer() >= 0
28
29              Time in milliseconds.
30
31       tref()
32
33              A timer reference.
34

EXPORTS

36       apply_after(Time, Module, Function, Arguments) ->
37                      {ok, TRef} | {error, Reason}
38
39              Types:
40
41                 Time = time()
42                 Module = module()
43                 Function = atom()
44                 Arguments = [term()]
45                 TRef = tref()
46                 Reason = term()
47
48              Evaluates spawn(Module, Function,  Arguments)  after  Time  mil‐
49              liseconds.
50
51              Returns {ok, TRef} or {error, Reason}.
52
53       apply_interval(Time, Module, Function, Arguments) ->
54                         {ok, TRef} | {error, Reason}
55
56              Types:
57
58                 Time = time()
59                 Module = module()
60                 Function = atom()
61                 Arguments = [term()]
62                 TRef = tref()
63                 Reason = term()
64
65              Evaluates  spawn(Module,  Function, Arguments) repeatedly at in‐
66              tervals of Time, irrespective of whether  a  previously  spawned
67              process has finished or not.
68
69          Warning:
70              If  the  execution  time  of the spawned process is, on average,
71              greater than the given Time, multiple such processes will run at
72              the  same  time. With long execution times, short intervals, and
73              many interval timers running, this may even  lead  to  exceeding
74              the number of allowed processes. As an extreme example, consider
75              [timer:apply_interval(1,  timer,  sleep,   [1000])   ||   _   <-
76              lists:seq(1,  1000)], that is, 1,000 interval timers executing a
77              process that takes 1s to complete, started in intervals of  1ms,
78              which  would  result  in 1,000,000 processes running at the same
79              time, far more than a node started with default settings  allows
80              (see the System Limits section in the Effiency Guide).
81
82
83              Returns {ok, TRef} or {error, Reason}.
84
85       apply_repeatedly(Time, Module, Function, Arguments) ->
86                           {ok, TRef} | {error, Reason}
87
88              Types:
89
90                 Time = time()
91                 Module = module()
92                 Function = atom()
93                 Arguments = [term()]
94                 TRef = tref()
95                 Reason = term()
96
97              Evaluates  spawn(Module,  Function, Arguments) repeatedly at in‐
98              tervals of Time, waiting for the spawned process to  finish  be‐
99              fore starting the next.
100
101              If the execution time of the spawned process is greater than the
102              given Time, the next process is spawned  immediately  after  the
103              one  currently  running  has  finished.  Assuming that execution
104              times of the spawned processes performing the applies on average
105              are  smaller  than Time, the amount of applies made over a large
106              amount of time will be the same even if some  individual  execu‐
107              tion times are larger than Time. The system will try to catch up
108              as soon as possible. For example, if one apply  takes  2.5*Time,
109              the following two applies will be made immediately one after the
110              other in sequence.
111
112              Returns {ok, TRef} or {error, Reason}.
113
114       cancel(TRef) -> {ok, cancel} | {error, Reason}
115
116              Types:
117
118                 TRef = tref()
119                 Reason = term()
120
121              Cancels a previously requested time-out. TRef is a unique  timer
122              reference returned by the related timer function.
123
124              Returns  {ok,  cancel},  or  {error,  Reason} when TRef is not a
125              timer reference.
126
127       exit_after(Time, Reason1) -> {ok, TRef} | {error, Reason2}
128
129       exit_after(Time, Target, Reason1) -> {ok, TRef} | {error, Reason2}
130
131              Types:
132
133                 Time = time()
134                 Target = pid() | (RegName :: atom())
135                 TRef = tref()
136                 Reason1 = Reason2 = term()
137
138              exit_after/2 is the same as exit_after(Time, self(), Reason1).
139
140              exit_after/3 sends an exit signal with reason Reason1 to Target,
141              which  can  be a local process identifier or an atom of a regis‐
142              tered name. Returns {ok, TRef} or {error, Reason2}.
143
144       hms(Hours, Minutes, Seconds) -> MilliSeconds
145
146              Types:
147
148                 Hours = Minutes = Seconds = MilliSeconds = integer() >= 0
149
150              Returns the number of milliseconds in Hours + Minutes + Seconds.
151
152       hours(Hours) -> MilliSeconds
153
154              Types:
155
156                 Hours = MilliSeconds = integer() >= 0
157
158              Returns the number of milliseconds in Hours.
159
160       kill_after(Time) -> {ok, TRef} | {error, Reason2}
161
162       kill_after(Time, Target) -> {ok, TRef} | {error, Reason2}
163
164              Types:
165
166                 Time = time()
167                 Target = pid() | (RegName :: atom())
168                 TRef = tref()
169                 Reason2 = term()
170
171              kill_after/1 is the same as exit_after(Time, self(), kill).
172
173              kill_after/2 is the same as exit_after(Time, Target, kill).
174
175       minutes(Minutes) -> MilliSeconds
176
177              Types:
178
179                 Minutes = MilliSeconds = integer() >= 0
180
181              Returns the number of milliseconds in Minutes.
182
183       now_diff(T2, T1) -> Tdiff
184
185              Types:
186
187                 T1 = T2 = erlang:timestamp()
188                 Tdiff = integer()
189                   In microseconds
190
191              Calculates the time difference Tdiff = T2 - T1 in  microseconds,
192              where  T1 and T2 are time-stamp tuples on the same format as re‐
193              turned from erlang:timestamp/0 or os:timestamp/0.
194
195       seconds(Seconds) -> MilliSeconds
196
197              Types:
198
199                 Seconds = MilliSeconds = integer() >= 0
200
201              Returns the number of milliseconds in Seconds.
202
203       send_after(Time, Message) -> {ok, TRef} | {error, Reason}
204
205       send_after(Time, Destination, Message) ->
206                     {ok, TRef} | {error, Reason}
207
208              Types:
209
210                 Time = time()
211                 Destination =
212                     pid() |
213                     (RegName :: atom()) |
214                     {RegName :: atom(), Node :: node()}
215                 Message = term()
216                 TRef = tref()
217                 Reason = term()
218
219                send_after/3:
220                  Evaluates Destination !  Message  after  Time  milliseconds.
221                  (Destination can be a remote or local process identifier, an
222                  atom of a registered name or a tuple {RegName, Node}  for  a
223                  registered name at another node.)
224
225                  Returns {ok, TRef} or {error, Reason}.
226
227                  See also  the Timer Module section in the Efficiency Guide.
228
229                send_after/2:
230                  Same as send_after(Time, self(), Message).
231
232       send_interval(Time, Message) -> {ok, TRef} | {error, Reason}
233
234       send_interval(Time, Destination, Message) ->
235                        {ok, TRef} | {error, Reason}
236
237              Types:
238
239                 Time = time()
240                 Destination =
241                     pid() |
242                     (RegName :: atom()) |
243                     {RegName :: atom(), Node :: node()}
244                 Message = term()
245                 TRef = tref()
246                 Reason = term()
247
248                send_interval/3:
249                  Evaluates  Destination  ! Message repeatedly after Time mil‐
250                  liseconds. (Destination can be a  remote  or  local  process
251                  identifier,  an  atom  of a registered name or a tuple {Reg‐
252                  Name, Node} for a registered name at another node.)
253
254                  Returns {ok, TRef} or {error, Reason}.
255
256                send_interval/2:
257                  Same as send_interval(Time, self(), Message).
258
259       sleep(Time) -> ok
260
261              Types:
262
263                 Time = timeout()
264
265              Suspends the process calling this function for Time milliseconds
266              and  then returns ok, or suspends the process forever if Time is
267              the atom infinity. Naturally, this function does not return  im‐
268              mediately.
269
270          Note:
271              Before OTP 25, timer:sleep/1 did not accept integer timeout val‐
272              ues greater than 16#ffffffff, that is, 2^32-1. Since OTP 25, ar‐
273              bitrarily high integer values are accepted.
274
275
276       start() -> ok
277
278              Starts  the  timer server. Normally, the server does not need to
279              be started explicitly.  It  is  started  dynamically  if  it  is
280              needed.  This is useful during development, but in a target sys‐
281              tem the server is to be started  explicitly.  Use  configuration
282              parameters for Kernel for this.
283
284       tc(Fun) -> {Time, Value}
285
286       tc(Fun, Arguments) -> {Time, Value}
287
288       tc(Module, Function, Arguments) -> {Time, Value}
289
290              Types:
291
292                 Module = module()
293                 Function = atom()
294                 Arguments = [term()]
295                 Time = integer()
296                 Value = term()
297
298                tc/3:
299                  Calls  function  timer:tc(Module,  Function,  Arguments, mi‐
300                  crosecond).
301
302                tc/2:
303                  Calls function timer:tc(Fun, Arguments, microsecond).
304
305                tc/1:
306                  Calls function timer:tc(Fun, microsecond).
307
308       tc(Fun, TimeUnit) -> {Time, Value}
309
310       tc(Fun, Arguments, TimeUnit) -> {Time, Value}
311
312       tc(Module, Function, Arguments, TimeUnit) -> {Time, Value}
313
314              Types:
315
316                 Module = module()
317                 Function = atom()
318                 Arguments = [term()]
319                 TimeUnit = erlang:time_unit()
320                 Time = integer()
321                   In the specified TimeUnit
322                 Value = term()
323
324                tc/4:
325                  Evaluates apply(Module, Function,  Arguments)  and  measures
326                  the   elapsed   real   time   as  reported  by  erlang:mono‐
327                  tonic_time/0.
328
329                  Returns {Time, Value}, where Time is the elapsed  real  time
330                  in  the  specified  TimeUnit,  and Value is what is returned
331                  from the apply.
332
333                tc/3:
334                  Evaluates apply(Fun, Arguments). Otherwise the same as tc/4.
335
336                tc/2:
337                  Evaluates Fun(). Otherwise the same as tc/3.
338

EXAMPLES

340       Example 1
341
342       The following example shows how to print "Hello World!" in 5 seconds:
343
344       1> timer:apply_after(5000, io, format, ["~nHello World!~n", []]).
345       {ok,TRef}
346       Hello World!
347
348       Example 2
349
350       The following example shows a process performing a certain action,  and
351       if  this action is not completed within a certain limit, the process is
352       killed:
353
354       Pid = spawn(mod, fun, [foo, bar]),
355       %% If pid is not finished in 10 seconds, kill him
356       {ok, R} = timer:kill_after(timer:seconds(10), Pid),
357       %% We change our mind...
358       timer:cancel(R),
359

NOTES

361       A timer can always be removed by calling cancel/1.
362
363       An interval timer, that is, a timer created by evaluating  any  of  the
364       functions  apply_interval/4,  send_interval/3,  and  send_interval/2 is
365       linked to the process to which the timer performs its task.
366
367       A one-shot timer, that is, a timer created by  evaluating  any  of  the
368       functions   apply_after/4,  send_after/3,  send_after/2,  exit_after/3,
369       exit_after/2, kill_after/2, and  kill_after/1  is  not  linked  to  any
370       process.  Hence, such a timer is removed only when it reaches its time-
371       out, or if it is explicitly removed by a call to cancel/1.
372
373
374
375Ericsson AB                      stdlib 5.1.1                         timer(3)
Impressum