DEV Community

Dmitry
Dmitry

Posted on

Why setInterval Dies at 30 Seconds in Manifest V3

The badge froze at 12:33. The session timer had been ticking every second in the background worker, and after I switched tabs for half a minute it simply stopped — no exception, no console error, just a stale number in the toolbar.

That "30 seconds" is not a limit on setInterval. It is the service worker's idle timeout, and it applies to everything the worker owns.

The short version

  • MV3 terminates the background service worker after roughly 30 seconds without extension events.
  • A setInterval callback is not an extension event, so it does not reset that clock.
  • Use chrome.alarms for anything that must outlive the worker; keep sub-minute animation in the UI context that owns it.

What the 30-second clock actually measures

Chrome starts an idle timer when the worker starts. Extension events and API calls reset it. When it expires, the worker is terminated and its whole JS heap — interval handles included — goes with it.

So the interval does not "stop after 30 seconds" on a schedule. It stops at the first moment the worker has been idle for 30 seconds, which in a quiet extension is almost immediately after the event that started it.

There is a second limit worth knowing: a single event handler can run for up to five minutes. That matters if you await something slow, but it does not keep an interval alive.

Timeline for a typical badge timer:

  • t=0 — a message arrives, the worker boots, setInterval(tick, 1000) is registered.
  • t=1..29 — ticks fire, each one updates the badge.
  • t=30 — no extension events for 30 seconds; the idle timer fires.
  • t=30+ — the worker is terminated. The next tick never happens, and the badge keeps the last value.

The table I wish I had read first

Primitive Survives worker restart Survives 30s idle Precision
setTimeout / setInterval No No milliseconds, while alive
chrome.alarms Yes Yes 30s minimum, approximate
Offscreen document timer No While the document is open milliseconds
Popup / side panel timer No While the UI is open milliseconds

Two rows deserve a note. An offscreen document is a real DOM context, so a timer there runs at full resolution — but the document can be closed, and Chrome allows only one at a time. A popup is the simplest place for a one-second display, because the user is looking at it; when the popup closes, the timer should stop anyway.

The migration that actually works

For a session timer, store the deadline, not the remaining seconds. The deadline survives everything; the number of seconds is derived.

const KEY = 'session-deadline';
const ALARM = 'session-tick';

async function startSession(minutes: number) {
  const deadline = Date.now() + minutes * 60_000;
  await chrome.storage.session.set({ [KEY]: deadline });
  await chrome.alarms.create(ALARM, { periodInMinutes: 0.5 });
  await render();
}

async function render() {
  const stored = await chrome.storage.session.get(KEY);
  const deadline = stored[KEY] as number | undefined;
  if (!deadline) return;

  const left = Math.max(0, deadline - Date.now());
  await chrome.action.setBadgeText({ text: format(left) });
  if (left === 0) await chrome.alarms.clear(ALARM);
}

chrome.alarms.onAlarm.addListener((alarm) => {
  if (alarm.name === ALARM) void render();
});
Enter fullscreen mode Exit fullscreen mode

The badge now updates at the alarm cadence — 30 seconds is the floor — not every second. For a countdown the user is watching, render in the popup with setInterval and recompute from the stored deadline on every tick. The worker only needs to survive long enough to answer.

Where this still hurts

chrome.alarms is approximate: a 30-second period can drift, and Chrome may batch alarms to save power. If your feature promises "exactly every 15 minutes", it will eventually break that promise. Design for "at least this often, approximately".

Alarms can also disappear on extension updates, so re-create them in chrome.runtime.onInstalled and onStartup. And if you need sub-30-second work while no UI is open, MV3 has no supported primitive for it. That is the point of the model, not an oversight.

If rewriting every timer by hand sounds familiar, vite-mv3-sw-compile-persist does it at build time: long setTimeout and setInterval calls become alarm-backed wrappers, and the generated runtime re-arms them after a restart. Destructuring declarations and anonymous callbacks that close over locals remain unsupported — it is a compiler, not magic.

With and without the plugin

The before/after from the plugin's README is the whole argument:

Without vite-mv3-sw-compile-persist

let count = 0;

chrome.action.onClicked.addListener(async () => {
  const stored = await chrome.storage.session.get('count');
  count = (stored.count as number) ?? 0;
  count++;
  await chrome.storage.session.set({ count });
});

chrome.alarms.create('cleanup', { delayInMinutes: 5 });
chrome.alarms.onAlarm.addListener((alarm) => {
  if (alarm.name === 'cleanup') cleanup();
});
Enter fullscreen mode Exit fullscreen mode

With vite-mv3-sw-compile-persist

let count = 0;

chrome.action.onClicked.addListener(() => {
  count++;
});

setTimeout(cleanup, 5 * 60_000);
Enter fullscreen mode Exit fullscreen mode

The second version is the code you meant to write. The plugin adds restore, dirty tracking, a debounced flush and alarm-backed timers at build time — no runtime import, no wrapper API.

Repository: vite-mv3-sw-compile-persist

If this explained the mystery behind your frozen badge, a star helps the next search land on the repo instead of another keepalive hack.

Is your timer counting down to a moment, or counting up for display? The answer decides where it should live.

Top comments (0)