-define(INTERVAL, 1000).
init([]) ->
Timer = erlang:send_after(?INTERVAL, self(), tick),
{ok, Timer}.
handle_info(tick, OldTimer) ->
erlang:cancel_timer(OldTimer),
io:format("Tick ~w~n", [OldTimer]),
Timer = erlang:send_after(?INTERVAL, self(), tick).
{noreplay, Timer}.
start_clock() ->
{_, Timer} = init([]),
spawn(clock, handle_info, [tick, Timer]).
My codes is as above, but the output is not what I want. How can I integrate init() and handle_info() into the main function(start_clock)?
I think you need something like this:
start_timer() ->
gen_server:start_link({local, clock}, ?MODULE, [], []).