Reputation: 407
Im trying to simply get this Erlang program to print out a value periodically and I'm using this module to do so:
-module(hot).
-export([start/0,handle_info/2, add/1]).
start() ->
Num = 0,
timer:send_interval(1000, interval),
{ok, Num}.
handle_info(interval, Num) ->
erlang:display(0),
{noreply, Num}.
add(X) ->
X+2.
I'm referencing one of the answers from this post
I also tried using a modification from the top answer as follows:
init([]) ->
Timer = erlang:send_after(1, self(), check),
{ok, Timer}.
handle_info(check, OldTimer) ->
erlang:cancel_timer(OldTimer),
erlang:display(0),
Timer = erlang:send_after(1000, self(), check),
{noreply, Timer}.
Nothing prints out in either case. It just tells me something like {ok,<0.43.0>}. It seems that handle_info is never called at all.
Upvotes: 0
Views: 484
Reputation:
Show the value of every second you can, for example, as follows:
-module(test).
-export([start/1]).
start(Num) when not is_number(Num)-> {error,not_a_number};
start(Num)->spawn(fun()->print(Num) end).
print(Num)->
erlang:start_timer(1000, self(), []),
receive
{timeout, Tref, _} ->
erlang:cancel_timer(Tref),
io:format("~p~n",[Num]),
if
Num =:=0 -> io:format("Stop~n");
true -> print(Num-1)
end
end.
For information about this function and its arguments, you can read the documentation. Such a method is more effecient than the use of the timer module.
Upvotes: 4