2

I'm building chrome extension, and in background script I use:

chrome.alarms.create({periodInMinutes: 1})

chrome.alarms.onAlarm.addListener(() => {
  doSomeStuff();
});

I do it few times, and in the last call, I do clearAll alarms.

The problem which I encounter in this situation is: My app works perfectly when Console is open. But if I close my console in Chrome, my app stops working.

My opinion is that when I open Console to inspect service worker service worker is awake, but when I close it my service worker goes into idle (sleep) mode.

How to solve it?

Predrag Davidovic
  • 1,411
  • 1
  • 17
  • 20
  • 2
    The code you've shown isn't a problem. The problem is elsewhere. Service worker lives for 30 seconds so you should use chrome.storage.local to read/write the state in the event listener, not global variables. Alternatively, you can force the worker to live longer, see [Persistent Service Worker in Chrome Extension](https://stackoverflow.com/a/66618269) – wOxxOm Jun 29 '21 at 07:07
  • Hi wOxxOm, I waited for your answer :) Everything what I read about chrome extensions was your answers. I will try to explain it better with more code. – Predrag Davidovic Jun 29 '21 at 07:14
  • Hi @wOxxOm you got the point without looking into code. I really relied on global variables for some counting in alarms. Thank you a lot, you solved my problem. – Predrag Davidovic Jun 29 '21 at 07:26

0 Answers0