Replace setTimeout/setInterval with Alarms

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

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

" The time the alarm will fire first, given as milliseconds since the epoch."

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?

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” :slight_smile::
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/warmup

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

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

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.

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

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?