Go has very utilitarian ticker methods, for instance check: https://gobyexample.com/tickers
1 | package main |
These are very nice for running code at every interval. If you want something like this in Elixir, it can be implemented in a few lines of code.
This code allows you to create a Ticker
process by calling Ticker.start
with a recipient_pid
which is the process which receives the :tick
events, a tick_interval
to specify how often
a :tick
event should be sent to the recipient_pid and finally a duration
whose default is
:infinity
which means it will just keep ticking till the end of time. Once you set this up,
the recipient will keep getting :tick
events for every tick_interval
.
Go ahead and read the code, it is pretty self explanatory.
There is also erlang’s :timer.apply_interval(time, module, function, arguments)
which will run
some code for every interval of time
. However, the code below doesn’t create overlapping executions.
I have also created a gist in the interest of collaboration here: https://gist.github.com/minhajuddin/064226d73d5648aa73364218e862a497
1 | defmodule Ticker do |