JSPM

worker-timers

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 535844
  • Score
    100M100P100Q188621F
  • License MIT

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

Package Exports

  • worker-timers

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (worker-timers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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);