Skip to content

Polling

Pause polling when the page is hidden.

Implementation

ts
let interval: ReturnType<typeof setInterval> | undefined;

function startPolling() {
  interval ??= setInterval(fetchLatest, 5_000);
}

function stopPolling() {
  if (interval) clearInterval(interval);
  interval = undefined;
}

lifecycle.on("page:visible", startPolling);
lifecycle.on("page:hidden", stopPolling);

Playground

Visibility Playground

JOC is a JayOnCode monorepo of @jayoncode/* packages. Docs sync from source via TypeDoc and sync scripts.