/worker-timers

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

Primary LanguageJavaScript

worker-timers

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

tests dependencies 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 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:

var workerTimers = require('worker-timers');

The usage is exactly the same as with the corresponding functions on the global scope.

var intervalId = workerTimers.setInterval(function () {
    // do something many times
}, 100);

workerTimers.clearInterval(intervalId);

var timeoutId = workerTimers.setTimeout(function () {
    // do something once
}, 100);

workerTimers.clearTimeout(timeoutId);