set-timeout-workerdeprecated

A dedicated web-worker for the `setTimeout` method. Client only. No server required.

Usage no npm install needed!

<script type="module">
  import setTimeoutWorker from 'https://cdn.skypack.dev/set-timeout-worker';
</script>

README

License: MIT Build Status

set-timeout-worker

A dedicated web-worker for the setTimeout method. Client only. No server required.

 

Why?

When the current tab loses focus the browser throttles its timeouts and they become inaccurate when setting short timeout periods. This behavior is inconsistent between different browsers and the subject is not well documented.

Using a web-worker for setting timeouts eliminates this issue.

 

Install

$ npm install set-timeout-worker
or
$ yarn add set-timeout-worker

 

Usage

// import:
import { setTimeoutWorker } from 'set-timeout-worker';
// or require:
const { setTimeoutWorker } = require('set-timeout-worker');

// initialize
setTimeoutWorker.start();

const timeoutRef = setTimeoutWorker.setTimeout(() => {
    // do somthing
}, 3000)

setTimeoutWorker.clearTimeout(timeoutRef);

// terminate
setTimeoutWorker.stop();

 

setTimeoutWorker

A singleton instance. It has the following methods:

  • .start()
  • .setTimeout(callback, ms)
  • .clearTimeout(timeoutRef)
  • .stop()
  • .onError(errorHandler)

 

.start()

Initializes a worker. Must be called in order to set timeouts.

 

.setTimeout(callback, ms)

.setTimeout(callback, ms[, arg1, arg2, ..., argN])

TL;DR - Same as window.setTimeout().

Sets a timeout in the worker scope. Returns a timeout reference (number) that can be used for clearing the timeout. Accepts a callback to run after the given delay in milliseconds. Extra arguments will be passed to the callback by the same order.

Calling setTimeout before initializing a worker will throw an error. See .start() above.

setTimeoutWorker.setTimeout((a, b, c) => {

    console.log(a, b, c); // 1 2 3

}, 1000, 1, 2, 3);

 

.clearTimeout()

TL;DR - Same as window.clearTimeout().

Cancles an active timeout. Accept a timeout reference (returned by setTimeout).

const timeoutRef = setTimeoutWorker.setTimeout(callback, 1000);

setTimeoutWorker.clearTimeout(timeoutRef);

 

.stop()

Terminates the worker, clearing any active timeouts. Will not set any new timeouts until .start() is called again.

 

.onError(errorHandler)

Sets an error handler function to catch the worker's exceptions. The function will get called with an Error

setTimeoutWorker.onError((err) => {
    console.error(err);
});

/*
  Error: SetTimeoutWorker
  Uncaught Error: Something Happend!
      Worker: blob:null/ce72fad0-bd41-46f7-9bea-fd405ab117c5
      Line: 7
      Col: 15
      Timestamp: 72.00000001466833
      at Worker._worker.onerror (set-timeout-worker.js:58)
*/

You can see the worker's code by opening the blob in a new browser tab.