1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wslでelixir その115

Last updated at Posted at 2023-12-03

概要

wsl(wsl2じゃない)で、elixirやってみた。
タイマーどうすんだ。
練習問題、やってみた。

練習問題

Livebookで時計を作成せよ。

方針

  • genserver使う。
  • Process.send_after使う。

写真

image.png

サンプルコード

defmodule Example do
  use GenServer
  @ten_seconds 10000
  def init(opts) do
    Process.send_after(self(), :tick, @ten_seconds)
    {:ok, opts}
  end
  def handle_info(:tick, state) do
    time = DateTime.utc_now()
      |> DateTime.to_time()
      |> Time.to_iso8601()
    IO.puts("The time is now: #{time}")
    Process.send_after(self(), :tick, @ten_seconds)
    {:noreply, state}
  end
end

GenServer.start(Example, [])

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?