setInterval Timer alternatives

453 views
Skip to first unread message

joette cordsen

unread,
Oct 25, 2023, 8:07:16 AM10/25/23
to Chromium Extensions
Much has been written about this but a lot of it is old, so it may no longer be relevant. I have a content script that runs a countdown timer.  When the timer is running the countdown is displayed in the tab header and when the time is up a sound is played. The interval is 1 second.  Since moving to manifest V3 I have heard from more users about the timer stopping or being unreliable when the tab is not in focus. I understand that is because of throttling on timers for unfocused tabs. I have thought about having a service worker keep time for the tab, but I have also read that maybe timers are throttled in service workers? What is the current thinking on this in terms of the best way to get the timer in a tab that is not in focus to keep running?.  It doesn't happen all the time,  and happens more for users that are doing other intensive processing on their computer. There is a flag  "Throttle non-visible cross-origin iframes" that I disabled, that does make a difference, but not 100% of the time. The reality is, even if it worked 100% of the time, I can't expect my users to all disable it. Any suggestions? 

TIA.

wOxxOm

unread,
Oct 25, 2023, 8:13:28 AM10/25/23
to Chromium Extensions, joette cordsen
1. Throttling of timers in web pages is not MV3-specific.
2. Timers in the extension service worker are not throttled but you'll have to force the script to stay alive for the duration of the countdown.

Oliver Dunk

unread,
Oct 25, 2023, 8:29:09 AM10/25/23
to wOxxOm, Chromium Extensions, joette cordsen
Hi joette,

Thanks for the question!

I forget if it directly covers this problem but you might be interested in this video from a while back: https://www.youtube.com/watch?v=MCi6AZMkxcU&pp=ygUOdGltZXJzIGh0dHAyMDM%3D

To keep the service worker alive, you could open a long-lived message port with `chrome.runtime.connect`. If you send a message across this channel every second the service worker shouldn't be terminated, since that resets the idle timer.

Hope that helps,
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/07cf4ccb-4dad-4d23-b7b0-efc57de78e1en%40chromium.org.

wOxxOm

unread,
Oct 25, 2023, 8:42:38 AM10/25/23
to Chromium Extensions, Oliver Dunk, Chromium Extensions, joette cordsen, wOxxOm
+1, using port is the best keep-alive method here.
Reply all
Reply to author
Forward
0 new messages