npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@vorthain/tab-safe-timers

v0.2.6

Published

Web Worker-powered timer functions that continue running accurately when browser tabs are in the background

Downloads

17

Readme

⏱️ @vorthain/tab-safe-timers

npm Downloads Bundle Size

Web Worker-powered timer functions that continue running accurately when browser tabs are in the background. Zero configuration required!

Why?

Modern browsers throttle JavaScript timers (setTimeout, setInterval) when tabs are in the background to save battery and improve performance. This can break functionality that depends on accurate timing, such as:

  • Real-time applications
  • Game loops
  • Analytics tracking
  • Session management
  • Periodic data syncing
  • Countdown timers

This library replaces the native timer functions with Web Worker-based implementations that run in a separate thread, unaffected by background tab throttling.

Installation

npm install @vorthain/tab-safe-timers

Usage

import { initTabSafeTimers } from '@vorthain/tab-safe-timers';

// Initialize once at app startup
initTabSafeTimers();

// Your existing timer code now works in background tabs!
setInterval(() => {
  console.log('This runs every second, even in background tabs!');
}, 1000);

That's it! No configuration files, no worker setup, no build steps required.

Examples

React

// App.js
import { useEffect } from 'react';
import { initTabSafeTimers, destroyTabSafeTimers } from '@vorthain/tab-safe-timers';

function App() {
  useEffect(() => {
    // Initialize on mount
    initTabSafeTimers();

    // Cleanup on unmount (optional)
    return () => {
      destroyTabSafeTimers();
    };
  }, []);

  // Use timers normally anywhere in your app
  useEffect(() => {
    const interval = setInterval(() => {
      console.log('Background-safe timer!');
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return <div>Your app</div>;
}

Vue

// main.js
import { createApp } from 'vue';
import { initTabSafeTimers } from '@vorthain/tab-safe-timers';
import App from './App.vue';

// Initialize before creating app
initTabSafeTimers();

createApp(App).mount('#app');

Next.js

// pages/_app.js or app/layout.js
import { useEffect } from 'react';
import { initTabSafeTimers } from '@vorthain/tab-safe-timers';

export default function App({ Component, pageProps }) {
  useEffect(() => {
    initTabSafeTimers();
  }, []);

  return <Component {...pageProps} />;
}

API

initTabSafeTimers()

Initializes the tab-safe timer system. Call this once when your application starts.

Returns: TabSafeTimers instance

Throws: Error if initialization fails (e.g., not in a browser environment or Web Workers not supported)

// Simple usage
initTabSafeTimers();

// With error handling
try {
  initTabSafeTimers();
} catch (error) {
  console.warn('Tab-safe timers not available:', error);
  // Your app still works with regular timers
}

destroyTabSafeTimers()

Destroys the timer system and restores native timer functions. This is optional - you only need to call this if you want to clean up resources or disable the tab-safe functionality.

destroyTabSafeTimers();

getTabSafeTimers()

Returns the current TabSafeTimers instance or null if not initialized.

const instance = getTabSafeTimers();
if (instance) {
  console.log('Tab-safe timers are active');
}

How It Works

  1. The library embeds the Web Worker code as a JavaScript string
  2. On initialization, it creates a Blob URL from this code and spawns a Worker
  3. The Worker runs in a separate thread, unaffected by background throttling
  4. Timer functions are globally overridden to communicate with the Worker
  5. Your callbacks are stored and executed when the Worker sends tick messages

Browser Compatibility & Limitations

Desktop Browsers

Works perfectly - Web Workers bypass background tab throttling completely on desktop browsers (Chrome, Firefox, Safari, Edge).

Mobile Browsers

⚠️ Works most of the time but with caveats:

  • OS-level throttling: iOS Safari and some Android browsers may suspend background tabs entirely after a certain time to save battery. When this happens, the worker is paused completely until the tab becomes active again.
  • Memory pressure: Mobile browsers may stop workers if the device is low on memory or in battery saver mode.
  • Low-power modes: System-wide power saving features can override browser behavior.

Bottom line: This library solves the vast majority of timer-throttling issues, but nothing in JavaScript can guarantee 100% uptime in background mobile tabs due to OS-level restrictions.

Common Use Cases

Accurate Time Tracking

initTabSafeTimers();

let secondsElapsed = 0;
setInterval(() => {
  secondsElapsed++;
  updateUI(secondsElapsed);
}, 1000); // Stays accurate even in background

Session Management

initTabSafeTimers();

// Check session every 30 seconds, even in background tabs
setInterval(async () => {
  const response = await fetch('/api/session/heartbeat');
  if (!response.ok) {
    handleSessionExpired();
  }
}, 30000);

Real-time Data Syncing

initTabSafeTimers();

// Keep data fresh even when user switches tabs
setInterval(async () => {
  const data = await fetchLatestData();
  updateStore(data);
}, 5000);

TypeScript

The package includes complete TypeScript definitions:

import { initTabSafeTimers, destroyTabSafeTimers, getTabSafeTimers } from '@vorthain/tab-safe-timers';

// Full type safety
const instance = initTabSafeTimers();