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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
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
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Other popular topics
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
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
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
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









