10LOC

#page-visibility

Web Platformintermediate

Page Visibility API to pause work in a backgrounded tab

export const pauseWhenHidden = (start: () => () => void) => {
  let stop = start();

  document.addEventListener("visibilitychange", () => {
    if (document.visibilityState === "hidden") {

Stop polling, ticking, or rendering the instant a tab goes to the background, and resume with the same start function when it comes back.