This package has been deprecated

Author message:

This package has been renamed to 'timeout-worker'. Use 'timeout-worker' instead.

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

1.0.3 • Public • Published

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.

Package Sidebar

Install

npm i set-timeout-worker

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

32.4 kB

Total Files

27

Last publish

Collaborators

  • taitulism