title: How do I call a function periodically in a manifest v3 Chrome extension?
body: <p><a href="https://developer.chrome.com/docs/extensions/mv3/intro/mv3-overview/#service-workers" rel="nofollow noreferrer">Service workers replace background pages in manifest v3 chrome extensions</a>, and I'm trying to use one for my extension. I need to be able to run a function periodically, and <a href="https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/#alarms" rel="nofollow noreferrer">it looks like alarms are the way to go</a>. In the example they recommend doing this in the top level of the service worker:</p>
  <pre class="lang-js prettyprint-override"><code>chrome.alarms.create({ delayInMinutes: 3.0 });
  
  chrome.alarms.onAlarm.addListener(() =&gt; {
    chrome.action.setIcon({
      path: getRandomIconPath(),
    });
  });
  </code></pre>
  <p>From what I understand though, weather or not my service worker gets killed in between events is non-deterministic. I believe if the browser kills my service worker this will be called every 3 minutes, since when the script is re-started to handle the alarm, it will run the first line again and queue up another alarm.</p>
  <p>By contrast, if the browser lets my service worker live for the 3 minutes in between alarms this won't loop, because it will only call <code>addListener()</code> once, but will call the callback twice (once for the first alarm that originally spawned this service worker, and again for the alarm registered on the first line of this invocation of the service worker). The service worker will then eventually die and no alarm will ever wake it again.</p>
  <p>Am I misunderstanding how events work here? If not, how do I register a re-occurring alarm once <code>chrome.alarms.create({ periodInMinutes: 3.0 });</code> and avoid re-registering it every time my service worker is re-started?</p>
  <p>NOTE: <code>delayInMinutes</code> fires once, <code>periodInMinutes</code> is re-ocuring.</p>