0

This is somewhat a code/browser specific question, but I noticed that my setTimeout/setInterval code will eventually NEVER execute in the background even if the tab is not asleep. The following logs are from code that is supposed to execute every 1s and I'd be fine bumping that to ~15s which would work with Firefox, but not to every 3 minutes like Chrome/Edge seem to demand:

Took 1.6927670464932056 time to execute at Tue Mar 07 2023 15:03:26 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.6322318190016232 time to execute at Tue Mar 07 2023 15:03:29 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.6322318190016232 time to execute at Tue Mar 07 2023 15:03:29 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.5268028376189324 time to execute at Tue Mar 07 2023 15:03:31 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.5268028376189324 time to execute at Tue Mar 07 2023 15:03:31 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4788079994765282 time to execute at Tue Mar 07 2023 15:03:34 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4788079994765282 time to execute at Tue Mar 07 2023 15:03:34 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4304450467780012 time to execute at Tue Mar 07 2023 15:03:36 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4304450467780012 time to execute at Tue Mar 07 2023 15:03:36 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4040571259142123 time to execute at Tue Mar 07 2023 15:03:46 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4040571259142123 time to execute at Tue Mar 07 2023 15:03:46 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.428811537802659 time to execute at Tue Mar 07 2023 15:07:16 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.428811537802659 time to execute at Tue Mar 07 2023 15:07:16 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4115845539280958 time to execute at Tue Mar 07 2023 15:10:16 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4115845539280958 time to execute at Tue Mar 07 2023 15:10:16 GMT-0700 (Mountain Standard Time)

It gets progressively longer until eventually it is basically stopped. This is a game intended to run in the background so any settings/code that can work around this would be great.

Edit: What ended up working was to add in some background music. The volume has to be non-zero, but users can just mute the tab and background tasks will still work.

  • Please use [`chrome.offscreen`](https://developer.chrome.com/docs/extensions/reference/offscreen/). – Norio Yamamoto Mar 07 '23 at 22:27
  • @NorioYamamoto this is for a web app, not a Chrome extension. – Dyllon Gagnier Mar 07 '23 at 23:14
  • May I know if you have got any chance to check my answer below? I am glad to help if you have any other questions. – Yu Zhou Mar 13 '23 at 07:23
  • @YuZhou it's been a while, but it seemed like the worker solution still got throttled. The only thing that worked was adding background music (even if the user muted the tab). It seems like browsers also don't like sites using workers to run CPU intensive workloads in the background. – Dyllon Gagnier Jun 23 '23 at 22:24

1 Answers1

1

The behavior is by design in chromium which is called tab throttling feature. The workaround is using Web Workers which runs in the background and not affected by throttling. For more information, you can also refer to this thread.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22