worker-timers
A replacement for setInterval() and setTimeout() which works in unfocused windows.
Last updated 5 years ago by chrisguttandin .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install worker-timers 
SYNC missed versions from official npm registry.

logo

worker-timers

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

version

Motivation

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 at which they invoke 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 worker-timers does.

Getting Started

worker-timers is available as a package on npm. Run the following command to install it:

npm install worker-timers

You can then import the exported functions in your code like this:

import { clearInterval, clearTimeout, setInterval, setTimeout } 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 = setInterval(() => {
    // do something many times
}, 100);

clearInterval(intervalId);

var timeoutId = setTimeout(() => {
    // do something once
}, 100);

clearTimeout(timeoutId);

Differentiation between Intervals and Timeouts

The native WindowTimers only maintain a single list of timers. But worker-timers maintains two separate lists to store the ids of intervals and timeouts internally. WindowTimers allows intervals to be cancelled by calling clearTimeout() and the other way round because it stores all timers in a single list. This is not possible with worker-timers.

const periodicWork = () => {};

// This will stop the interval.
const windowId = window.setInterval(periodicWork, 100);
window.clearTimeout(windowId);

// This will not cancel the interval. It may cancel a timeout.
const workerId = setInterval(periodicWork, 100);
clearTimeout(workerId);

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. The replacement should be straightforward, 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 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 worker-timers is used inside of an Angular app and Zone.js (which is the default) is used to detect changes, the behavior of worker-timers can be confusing. Angular is using Zone.js which is patching the native setInterval() and setTimeout() functions to get notified about the invocation of their callback functions. But Angular (more specifically Zone.js) is not aware of worker-timers and doesn't get notified about any callback invocations. Therefore Angular needs to be notified manually about state changes that occur inside of a callback function which was scheduled with the help of worker-timers.

Current Tags

  • 8.0.0                                ...           latest (6 months ago)

241 Versions

  • 8.0.0                                ...           6 months ago
  • 7.1.8                                ...           6 months ago
  • 7.1.7                                ...           7 months ago
  • 7.1.6                                ...           8 months ago
  • 7.1.5                                ...           8 months ago
  • 7.1.4                                ...           8 months ago
  • 7.1.3                                ...           8 months ago
  • 7.1.2                                ...           9 months ago
  • 7.1.1                                ...           10 months ago
  • 7.1.0                                ...           10 months ago
  • 7.0.80                                ...           a year ago
  • 7.0.79                                ...           a year ago
  • 7.0.78                                ...           a year ago
  • 7.0.77                                ...           a year ago
  • 7.0.76                                ...           a year ago
  • 7.0.75                                ...           a year ago
  • 7.0.74                                ...           a year ago
  • 7.0.73                                ...           a year ago
  • 7.0.72                                ...           a year ago
  • 7.0.71                                ...           a year ago
  • 7.0.70                                ...           a year ago
  • 7.0.69                                ...           a year ago
  • 7.0.68                                ...           a year ago
  • 7.0.67                                ...           a year ago
  • 7.0.66                                ...           a year ago
  • 7.0.65                                ...           2 years ago
  • 7.0.64                                ...           2 years ago
  • 7.0.63                                ...           2 years ago
  • 7.0.62                                ...           2 years ago
  • 7.0.61                                ...           2 years ago
  • 7.0.60                                ...           2 years ago
  • 7.0.59                                ...           2 years ago
  • 7.0.58                                ...           2 years ago
  • 7.0.57                                ...           2 years ago
  • 7.0.56                                ...           2 years ago
  • 7.0.55                                ...           2 years ago
  • 7.0.54                                ...           2 years ago
  • 7.0.53                                ...           2 years ago
  • 7.0.52                                ...           2 years ago
  • 7.0.51                                ...           2 years ago
  • 7.0.50                                ...           2 years ago
  • 7.0.49                                ...           2 years ago
  • 7.0.48                                ...           2 years ago
  • 7.0.47                                ...           3 years ago
  • 7.0.46                                ...           3 years ago
  • 7.0.45                                ...           3 years ago
  • 7.0.44                                ...           3 years ago
  • 7.0.43                                ...           3 years ago
  • 7.0.42                                ...           3 years ago
  • 7.0.41                                ...           3 years ago
  • 7.0.40                                ...           3 years ago
  • 7.0.39                                ...           3 years ago
  • 7.0.38                                ...           3 years ago
  • 7.0.37                                ...           3 years ago
  • 7.0.36                                ...           3 years ago
  • 7.0.35                                ...           3 years ago
  • 7.0.34                                ...           3 years ago
  • 7.0.33                                ...           3 years ago
  • 7.0.32                                ...           3 years ago
  • 7.0.31                                ...           3 years ago
  • 7.0.30                                ...           3 years ago
  • 7.0.29                                ...           4 years ago
  • 7.0.28                                ...           4 years ago
  • 7.0.27                                ...           4 years ago
  • 7.0.26                                ...           4 years ago
  • 7.0.25                                ...           4 years ago
  • 7.0.24                                ...           4 years ago
  • 7.0.23                                ...           4 years ago
  • 7.0.22                                ...           4 years ago
  • 7.0.21                                ...           4 years ago
  • 7.0.20                                ...           4 years ago
  • 7.0.19                                ...           4 years ago
  • 7.0.18                                ...           4 years ago
  • 7.0.17                                ...           4 years ago
  • 7.0.16                                ...           4 years ago
  • 7.0.15                                ...           4 years ago
  • 7.0.14                                ...           4 years ago
  • 7.0.13                                ...           4 years ago
  • 7.0.12                                ...           4 years ago
  • 7.0.11                                ...           4 years ago
  • 7.0.10                                ...           4 years ago
  • 7.0.9                                ...           4 years ago
  • 7.0.8                                ...           4 years ago
  • 7.0.7                                ...           4 years ago
  • 7.0.6                                ...           4 years ago
  • 7.0.5                                ...           4 years ago
  • 7.0.4                                ...           4 years ago
  • 7.0.3                                ...           4 years ago
  • 7.0.2                                ...           4 years ago
  • 7.0.1                                ...           4 years ago
  • 7.0.0                                ...           4 years ago
  • 6.0.23                                ...           4 years ago
  • 6.0.22                                ...           4 years ago
  • 6.0.21                                ...           4 years ago
  • 6.0.20                                ...           4 years ago
  • 6.0.19                                ...           4 years ago
  • 6.0.18                                ...           4 years ago
  • 6.0.17                                ...           4 years ago
  • 6.0.16                                ...           4 years ago
  • 6.0.15                                ...           4 years ago
  • 6.0.14                                ...           4 years ago
  • 6.0.13                                ...           4 years ago
  • 6.0.12                                ...           4 years ago
  • 6.0.11                                ...           5 years ago
  • 6.0.10                                ...           5 years ago
  • 6.0.9                                ...           5 years ago
  • 6.0.8                                ...           5 years ago
  • 6.0.7                                ...           5 years ago
  • 6.0.6                                ...           5 years ago
  • 6.0.5                                ...           5 years ago
  • 6.0.4                                ...           5 years ago
  • 6.0.3                                ...           5 years ago
  • 6.0.2                                ...           5 years ago
  • 6.0.1                                ...           5 years ago
  • 6.0.0                                ...           5 years ago
  • 5.1.9                                ...           5 years ago
  • 5.1.8                                ...           5 years ago
  • 5.1.7                                ...           5 years ago
  • 5.1.6                                ...           5 years ago
  • 5.1.5                                ...           5 years ago
  • 5.1.4                                ...           5 years ago
  • 5.1.3                                ...           5 years ago
  • 5.1.2                                ...           5 years ago
  • 5.1.1                                ...           5 years ago
  • 5.1.0                                ...           5 years ago
  • 5.0.36                                ...           5 years ago
  • 5.0.35                                ...           5 years ago
  • 5.0.34                                ...           5 years ago
  • 5.0.33                                ...           5 years ago
  • 5.0.32                                ...           5 years ago
  • 5.0.31                                ...           5 years ago
  • 5.0.30                                ...           5 years ago
  • 5.0.29                                ...           5 years ago
  • 5.0.28                                ...           5 years ago
  • 5.0.27                                ...           5 years ago
  • 5.0.26                                ...           5 years ago
  • 5.0.25                                ...           5 years ago
  • 5.0.24                                ...           5 years ago
  • 5.0.23                                ...           5 years ago
  • 5.0.22                                ...           5 years ago
  • 5.0.21                                ...           5 years ago
  • 5.0.20                                ...           5 years ago
  • 5.0.19                                ...           5 years ago
  • 5.0.18                                ...           5 years ago
  • 5.0.17                                ...           5 years ago
  • 5.0.16                                ...           6 years ago
  • 5.0.15                                ...           6 years ago
  • 5.0.14                                ...           6 years ago
  • 5.0.13                                ...           6 years ago
  • 5.0.12                                ...           6 years ago
  • 5.0.11                                ...           6 years ago
  • 5.0.10                                ...           6 years ago
  • 5.0.9                                ...           6 years ago
  • 5.0.8                                ...           6 years ago
  • 5.0.7                                ...           6 years ago
  • 5.0.6                                ...           6 years ago
  • 5.0.5                                ...           6 years ago
  • 5.0.4                                ...           6 years ago
  • 5.0.3                                ...           6 years ago
  • 5.0.2                                ...           6 years ago
  • 5.0.1                                ...           6 years ago
  • 5.0.0                                ...           6 years ago
  • 4.0.53                                ...           6 years ago
  • 4.0.52                                ...           6 years ago
  • 4.0.51                                ...           6 years ago
  • 4.0.50                                ...           6 years ago
  • 4.0.49                                ...           6 years ago
  • 4.0.48                                ...           6 years ago
  • 4.0.47                                ...           6 years ago
  • 4.0.46                                ...           6 years ago
  • 4.0.45                                ...           6 years ago
  • 4.0.44                                ...           6 years ago
  • 4.0.43                                ...           6 years ago
  • 4.0.42                                ...           6 years ago
  • 4.0.41                                ...           6 years ago
  • 4.0.40                                ...           6 years ago
  • 4.0.39                                ...           6 years ago
  • 4.0.38                                ...           6 years ago
  • 4.0.37                                ...           6 years ago
  • 4.0.36                                ...           6 years ago
  • 4.0.35                                ...           6 years ago
  • 4.0.34                                ...           6 years ago
  • 4.0.33                                ...           6 years ago
  • 4.0.32                                ...           7 years ago
  • 4.0.31                                ...           7 years ago
  • 4.0.30                                ...           7 years ago
  • 4.0.29                                ...           7 years ago
  • 4.0.28                                ...           7 years ago
  • 4.0.27                                ...           7 years ago
  • 4.0.26                                ...           7 years ago
  • 4.0.25                                ...           7 years ago
  • 4.0.24                                ...           7 years ago
  • 4.0.23                                ...           7 years ago
  • 4.0.22                                ...           7 years ago
  • 4.0.21                                ...           7 years ago
  • 4.0.20                                ...           7 years ago
  • 4.0.19                                ...           7 years ago
  • 4.0.18                                ...           7 years ago
  • 4.0.17                                ...           7 years ago
  • 4.0.16                                ...           7 years ago
  • 4.0.15                                ...           7 years ago
  • 4.0.14                                ...           7 years ago
  • 4.0.13                                ...           7 years ago
  • 4.0.8                                ...           7 years ago
  • 4.0.7                                ...           7 years ago
  • 4.0.6                                ...           7 years ago
  • 4.0.5                                ...           8 years ago
  • 4.0.4                                ...           8 years ago
  • 4.0.3                                ...           8 years ago
  • 4.0.2                                ...           8 years ago
  • 4.0.1                                ...           8 years ago
  • 4.0.0                                ...           8 years ago
  • 3.0.6                                ...           8 years ago
  • 3.0.5                                ...           8 years ago
  • 3.0.4                                ...           8 years ago
  • 3.0.3                                ...           8 years ago
  • 3.0.2                                ...           8 years ago
  • 3.0.1                                ...           8 years ago
  • 3.0.0                                ...           8 years ago
  • 2.0.6                                ...           8 years ago
  • 2.0.4                                ...           8 years ago
  • 2.0.2                                ...           8 years ago
  • 2.0.1                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.0.2                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
  • 0.2.0                                ...           9 years ago
  • 0.1.8                                ...           9 years ago
  • 0.1.7                                ...           9 years ago
  • 0.1.6                                ...           9 years ago
  • 0.1.5                                ...           9 years ago
  • 0.1.4                                ...           9 years ago
  • 0.1.3                                ...           9 years ago
  • 0.1.2                                ...           9 years ago
  • 0.1.1                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
  • 0.0.4                                ...           9 years ago
  • 0.0.2                                ...           9 years ago
  • 0.0.1                                ...           10 years ago
  • 0.0.0                                ...           10 years ago
Maintainers (1)
Downloads
Total 4
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE