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.

Last Post!

rhcarvalho

rhcarvalho

@jdmarshall welcome to the Elixir Forum!

@LostKobrakai I just realized this conversation about socket.private in misplaced given this thread is about :timer.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here 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 records...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement