# Replace setTimeout/setInterval with Alarms

**URL:** <https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336>\
**Category:** Development\
**Tags:** development\
**Created:** [January 24, 2021, 2:49pm UTC](https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336 "2021-01-24T14:49:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PELock](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/pelock/32/26356_2.png) [@PELock](https://discourse.mozilla.org/u/PELock)\
**Post date:** [January 24, 2021, 2:49pm UTC](https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336/1 "2021-01-24T14:49:38Z")

</div>

I was using setTimeout and recently setInterval in my extension (in content scripts) to fire events every n-seconds, since FF does it & Chrome introduced additional timer throttling in the background tabs for nested timers I’m thinking about other ways to handle it.

How can I achieve reliable functionality using Chrome/FF Alarms API?

The documentation states I can set periods (intervals) in minutes only (anything else will be rounded up to 1 minute)

[https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/alarms/create](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/alarms/create)

It also states I can set EPOCH + n-milliseconds event timeouts:

" The time the alarm will fire first, given as [milliseconds since the epoch](https://en.wikipedia.org/wiki/Unix_time)."

So does it mean I cannot fire intervals in less than 1 minute periods?

But does it also mean I can fire single events in less than 1 minute periods using WHEN parameter (say EPOCH+100 ms)?

I’m confused.

My code workflow looks like this:

1. Find entry on a page in DOM structure
2. Read/Modify it
3. Do n-seconds delay
4. Scroll to the next item
5. Goto 1

How can I do it without setTimeout / setInterval for background tabs, will Alarms let me fire those events every n-seconds?

I have tried to use Workers with moderate success, but I’m not sure if that’s the right solution.

I think the right question is how can I achieve a reliable timer (with n-seconds precision) in my extension in foreground & background tabs/windows?

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [January 25, 2021, 8:46am UTC](https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336/2 "2021-01-25T08:46:20Z")

</div>

I’m pretty sure Alarms are for long-time events, that’s why the minute-based granularity. The setup with milliseconds from epoch is just to make things more flexible if you need to schedule something at the specific time.

Anyway, as long as your tab is active, your `setTimeout` should work fine.

Inactive tabs are throttled so not only your timeouts may be delayed but also DOM modification and other API can be delayed.  
You may be able to workaround this using the “warmup” API to keep tab “warm” 🙂:  
[https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/warmup](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/warmup)

---

<div class="post-metadata">

**Author:** ![PELock](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/pelock/32/26356_2.png) [@PELock](https://discourse.mozilla.org/u/PELock)\
**Post date:** [January 25, 2021, 1:32pm UTC](https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336/3 "2021-01-25T13:32:55Z")

</div>

I do too understand it’s for long time events, but what about short-time events like stock trading?

So there is no alternative for precise timers in background tabs? How is this good for any serious business app that is expected to be running the same in the active and background tab? This makes Firefox useless for many things…

Are Firefox and Chrome not capable of handling very precise timers in the name of “saving battery life”?

This **warmup** API, I will use it for FF (thank you), but what about Chrome, it doesn’t have it. So now do I have to use some hacky solutions to keep my extension compatible cross browsers?

So setTimeout & setInterval has been throttled (slowed down) in background tabs, and there are no extension API replacement for those?

How is that a good thing for anyone?

I’ve already seen a bunch of weird, hacky solutions for background tab timers, but I guess browsers should give us replacement mechanisms for those or just make it work as it supposes to work?

- timers in Android **aren’t** throttled
- timers in Windows **aren’t** throttled
- timers in Linux **aren’t** throttled
- timers in browser extensions **ARE**

---

<div class="post-metadata">

**Author:** ![hans\_squared](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/hans_squared/32/67424_2.png) [@hans\_squared](https://discourse.mozilla.org/u/hans_squared)\
**Post date:** [January 25, 2021, 4:57pm UTC](https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336/4 "2021-01-25T16:57:56Z")

</div>

> [@PELock](#):
>
> I’ve already seen a bunch of weird, hacky solutions for background tab timers

I’d suggest using Native Messaging, and letting the timer run in a native app.  
Was that one of the solutions?

---

<div class="post-metadata">

**Author:** ![freaktechnik](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/freaktechnik/32/37766_2.png) [@freaktechnik](https://discourse.mozilla.org/u/freaktechnik)\
**Post date:** [January 25, 2021, 5:16pm UTC](https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336/5 "2021-01-25T17:16:46Z")

</div>

If you’re worried about throttling, I’d assume you can always run the timers in the background page and use messaging, however the timing will be less accurate by then (same also applies to native messaging suggested above).

However, even if you manage to get an accurate timer for a background tab, its event loop may still be processed slower, so your actions will still happen slower. The only way around this would be to disable these resource optimizations the browser makes, which I’m not sure if that’s possible.

---

<div class="post-metadata">

**Author:** ![PELock](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/pelock/32/26356_2.png) [@PELock](https://discourse.mozilla.org/u/PELock)\
**Post date:** [January 25, 2021, 5:34pm UTC](https://discourse.mozilla.org/t/replace-settimeout-setinterval-with-alarms/74336/6 "2021-01-25T17:34:22Z")

</div>

> [@hans\_squared](#):
>
> Native Messaging,

Background page timers are cleared/throttled out from what I know

> **[Google Chrome tests throttling background JavaScript timers to improve...](https://www.xda-developers.com/google-chrome-tests-throttling-background-javascript-timers-improve-battery-life/)**
>
> Google Chrome is testing a new flag that throttles JavaScript timers to improve battery life on laptops by up to 2 hours.

and using Native Messaging looks like another hacky solutions.

I mean, is there no legitimate solution to do second like breaks within the extension code to work accurately both in active and background tabs?
