alain-nambi

alain-nambi

How to stop :timer

I have this code, i want to stop_clock right now with erlang :timer. How can I do this ? Is it possible ?

 def handle_event("start_clock", %{"id" => id}, socket) do
    :timer.send_interval(1000, self(), :tick)

    card = Kanban.get_card_from_modal!(id)

    estimated_duration = card.task.estimated_duration

    time = estimated_duration / 60
    hour = trunc(time)
    e = time - hour
    minute = round(e * 60)
    second = socket.assigns.second

    {:noreply, socket |> assign(data_card_id: card.id, time: estimated_duration ,hour: hour, minute: minute, second: second)}
  end

  def handle_event("stop_clock", %{"id" => id}, socket) do
    :timer.cancel(:tick)
   {:noreply, socket}
  end

Marked As Solved

alain-nambi

alain-nambi

I think that its works but I have tried :timer.kill_after(0) and it works too

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
iex(1)> :timer.kill_after(0)
** (EXIT from #PID<0.109.0>) shell process exited with reason: killed

I think that’s because it kills the current process, not because it’s actually killing your timer properly.

10
Post #5
rvirding

rvirding

Creator of Erlang

Creating timers using the functions :erlang.send_after/3 and :erlang.start_timer/3 is much more efficient than using the timers provided by the :timer module.

10
Post #6
LostKobrakai

LostKobrakai

Timers are not identified my the message they send, but the reference returned when starting the timer.

ref = :timer.send_interval(1000, self(), :tick)
:timer.cancel(ref)

In your case you’d need to retain the reference in your state, e.g. in socket.private.

rvirding

rvirding

Creator of Erlang

I think it because the :timer module is old and maybe predates the timer functions in the :erlang module. Originally it was a server running which managed all the timer requests and kept track of when to send messages and to whom. Now it has been heavily optimised and itself uses the timer functions n the :erlang module when it can so it is generally less efficient than calling the function directly. That would be one reason why it is still used in many examples. Also some of the functions like tc need some wrapping to get the same results.

EDIT: What I meant to say is that using :timer is generally almost as efficient as calling the timer functions directly. There is only a very small difference.

rvirding

rvirding

Creator of Erlang

There is an enormous number of cool functions in the :erlang module :smiley:

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement