Nucleus Powered Mitochondria

    TypeScript icon, indicating that this package has built-in type declarations

    7.0.47 • Public • Published



    A replacement for setInterval() and setTimeout() which works in unfocused windows.

    dependencies version


    For scripts that rely on WindowTimers like setInterval() or setTimeout() things get confusing when the site which the script is running on loses focus. Chrome, Firefox and maybe others throttle the frequency of firing those timers to a maximum of once per second in such a situation. However this is only true for the main thread and does not affect the behavior of Web Workers. Therefore it is possible to avoid the throttling by using a worker to do the actual scheduling. This is exactly what WorkerTimers do.

    Getting Started

    WorkerTimers are available as a package on npm. Simply run the following command to install it:

    npm install worker-timers

    You can then require the workerTimers instance from within your code like this:

    import * as workerTimers from 'worker-timers';

    The usage is exactly the same (despite of the error handling and the differentiation between intervals and timeouts) as with the corresponding functions on the global scope.

    var intervalId = workerTimers.setInterval(() => {
        // do something many times
    }, 100);
    var timeoutId = workerTimers.setTimeout(() => {
        // do something once
    }, 100);

    Error Handling

    The native WindowTimers are very forgiving. Calling clearInterval() or clearTimeout() without a value or with an id which doesn't exist will just get ignored. In contrast to that workerTimers will throw an error when doing so.

    // This will just return undefined.
    // This will throw an error.

    Differentiation between Intervals and Timeouts

    Another difference between workerTimers and WindowTimers is that this package maintains two separate lists to store the ids of intervals and timeouts internally. WindowTimers do only have one list which allows intervals to be cancelled by calling clearTimeout() and the other way round. This is not possible with workerTimers. As mentioned above workerTimers will throw an error when provided with an unknown id.

    const periodicWork = () => {};
    // This will stop the interval.
    const windowId = window.setInterval(periodicWork, 100);
    // This will throw an error.
    const workerId = workerTimers.setInterval(periodicWork, 100);

    Server-Side Rendering

    This package is intended to be used in the browser and requires the browser to have support for Web Workers. It does not contain any fallback which would allow it to run in another environment like Node.js which doesn't know about Web Workers. This is to prevent this package from silently failing in an unsupported browser. But it also means that it needs to be replaced when used in a web project which also supports server-side rendering. That should be easy, at least in theory, because each function has the exact same signature as its corresponding builtin function. But the configuration of a real-life project can of course be tricky. For a concrete example, please have a look at the worker-timers-ssr-example provided by @newyork-anthonyng. It shows the usage inside of a server-side rendered React app.

    Angular (& zone.js)

    If WorkerTimers are used inside of an Angular App and Zones are used to detect changes, the behavior of WorkerTimers can be confusing. Angular is using a Zone which is patching the native setInterval() and setTimeout() functions to get notified about the execution of their callback functions. But Angular (more specifically zone.js) is not aware of WorkerTimers and doesn't patch them. Therefore Angular needs to be notified manually about state changes that occur inside of a callback function which was scheduled with the help of WorkerTimers.


    npm i worker-timers

    DownloadsWeekly Downloads






    Unpacked Size

    26.9 kB

    Total Files


    Last publish


    • chrisguttandin