dwyl / dwyl/learn-elixir

Timers (or how to do `setInterval` and `clearInterval` in `Elixir` / `Erlang`?)

Open
#225 3 comments 0 reactions 1 assignee Claimed by @nelsonic View on GitHub
discuss elixir enhancement help wanted priority-2 question research T25m technical
Dominant language
Elixir
Stars
1.7k
Forks
112
PR merge metrics
No merged PRs in 30d

Description

I'm making a few _million_ `API` requests to get some data https://github.com/dwyl/who/issues/17
and need to _control_ the request frequency to avoid hitting **rate limits** ... ⏳ (**`5k/hour`** -> **`120k/day`** -> **`3.65m/month`** )
In `JS`-land I would just use `setInterval` to define the timeframe for executing the `API` requests
and `clearInterval` once they were complete.
Not had to do this in `Elixir` before, so doing a bit of googling ... 🔍

Reading:
https://stackoverflow.com/questions/43892784/what-are-the-equivalent-of-settimeout-and-setinterval-in-elixir

![image](https://github.com/user-attachments/assets/0b82a3db-91ac-4cd9-9207-4900f3cc7414)

The _recommended_ `Erlang` function is: [`timer.apply_interval/4`](https://www.erlang.org/doc/apps/stdlib/timer.html#apply_interval/2)

```sh
:timer.apply_interval(1000, IO, :puts, ["weee"])
```

https://www.erlang.org/doc/apps/stdlib/timer.html

Need to write myself a mini example ...

> **Side note**: notice how this question on `StackOverflow` that has _clearly_ been _useful_ to other people got **4 _downvotes_**:
![stackoverflow-downvotes-on-questions](https://github.com/user-attachments/assets/28155587-c897-492b-bedd-2cfda8853983)
yes, it doesn't conform to the narrow `SO` question guidelines, but it's still a _useful_ question/answer!
`SO` is just not a very welcoming (to beginners) environment. 😞
This in a nutshell is why `ChatGPT` which just repackages the web is so popular: it doesn't "judge" your ("stupid") questions!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.