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
Also Liked
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
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
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.
3
Last Post!
rhcarvalho
@jdmarshall welcome to the Elixir Forum!
@LostKobrakai I just realized this conversation about socket.private in misplaced given this thread is about :timer.
0
Popular in Questions
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
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I would like to know what is the best IDE for elixir development?
New
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
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
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
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
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...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
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
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









