View Source ExTimer (ex_timer v1.0.2)

ExTimer module.

Summary

Functions

add new timer to send msg after time milliseconds.

delete all the registerd timers.

return true if found the timer at the given msg, otherwise return false

get the minimum time(milliseconds) for the timer to expired.

delete the previous registerd timer.

call the callback handler (handle_info) for the timer that has elapsed.

Types

@type state() :: %{
  :elapsed_ticks => time_ms(),
  :timers => [timer_node()],
  optional(any()) => any()
}
@type time_ms() :: ExTimer.Node.time_ms()
@type timer_node() :: ExTimer.Node.t()
@type timer_node_msg() :: ExTimer.Node.msg()

Functions

Link to this function

add(state, msg, delta_ms)

View Source
@spec add(state(), timer_node_msg(), time_ms()) :: state()

add new timer to send msg after time milliseconds.

Examples

iex> state = %{timers: [], elapsed_ticks: 0}
%{elapsed_ticks: 0, timers: []}
iex> state = ExTimer.add(state, {:handler, :name, "uhaha"}, 2000)
%{elapsed_ticks: 0, timers: [%ExTimer.Node{delay: 2000, msg: {:handler, :name, "uhaha"}}]}
iex> [timer] = state.timers
[%ExTimer.Node{delay: 2000, msg: {:handler, :name, "uhaha"}}]
iex> timer.msg == {:handler, :name, "uhaha"}
true
iex> timer.delay == 2000
true
Link to this macro

clear(state, callback? \\ false)

View Source (macro)

delete all the registerd timers.

Examples

iex> state = %{timers: [%ExTimer.Node{msg: {:handler, :name, "uhaha"}, delay: 2000}], elapsed_ticks: 0}
%{elapsed_ticks: 0, timers: [%ExTimer.Node{delay: 2000, msg: {:handler, :name, "uhaha"}}]}
iex> ExTimer.clear(state)
%{timers: [], elapsed_ticks: 0}
@spec exist?(state(), timer_node_msg()) :: boolean()

return true if found the timer at the given msg, otherwise return false

Examples

iex> state = %{timers: [%ExTimer.Node{msg: {:handler, :name, "uhaha"}, delay: 2000}], elapsed_ticks: 0}
%{
  elapsed_ticks: 0,
  timers: [%ExTimer.Node{delay: 2000, msg: {:handler, :name, "uhaha"}}]
}
iex> ExTimer.exist?(state, {:handler, :name, "uhaha"})
true
Link to this function

next_expire_ticks(state, min_time)

View Source
@spec next_expire_ticks(state(), time_ms()) :: time_ms()

get the minimum time(milliseconds) for the timer to expired.

Examples

def handle_info(:tick, state) do
  now = System.system_time(:millisecond)
  delta_ticks = now - state.last_update_tick
  state = put_in(state.last_update_tick, now)
  state = ExTimer.update(state, delta_ticks)

  Process.send_after(self(), :tick, ExTimer.next_expire_ticks(state, 1000))
  {:noreply, state}
end
@spec remove(state(), timer_node_msg()) :: state()

delete the previous registerd timer.

Examples

iex> state = %{timers: [%ExTimer.Node{msg: {:handler, :name, "uhaha"}, delay: 2000}], elapsed_ticks: 0}
%{
  elapsed_ticks: 0,
  timers: [%ExTimer.Node{delay: 2000, msg: {:handler, :name, "uhaha"}}]
}
iex> ExTimer.remove(state, {:handler, :name, "uhaha"})
%{timers: [], elapsed_ticks: 0}
Link to this macro

update(state, delta_ms)

View Source (macro)

call the callback handler (handle_info) for the timer that has elapsed.

Examples

now = System.system_time(:millisecond)
delta_ticks = now - state.last_update_tick
state = put_in(state.last_update_tick, now)
state = ExTimer.update(state, delta_ticks)