browser-priority-timers
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

BrowserPriorityTimers

Browsers for optimisation throttles the tabs that are not visible. This slows down any timers the page is executing. When your application requires the timer to work reliably even in the background you can use the web workers as those are on separate thread and are not limited by aforementioned optimisation. The BrowserPriorityTimers class implements the timers API on the web worker thread.

Installation

Install library by calling

npm i browser-priority-timers

API

workerAvailable()

This class depends on the following features to be available on the window object: URL, Worker and Blob. If those are not available then the workerAvailable method will return false, also the setTimeout and setInterval will return -1 and will not work.

const timers = new BrowserPriorityTimers();

if(!timers.workerAvailable()) {
  console.warn("Priority timers are not available for this browser");
}

setInterval / clearInterval

const timers = new BrowserPriorityTimers();

if(timers.workerAvailable()) {
  const intervalId = timers.setInterval(() => {
    console.log("Tick...");
    timers.clearInterval(intervalId);
  }, 200);
}

setTimeout / clearTimeout

const timers = new BrowserPriorityTimers();

if(timers.workerAvailable()) {
  const intervalId = timers.setTimeout(() => {
    console.log("Tick...");
  }, 200);
  timers.clearTimeout(intervalId);
}

Acknowledgment

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i browser-priority-timers

Weekly Downloads

4

Version

0.3.0

License

MIT

Unpacked Size

14.3 kB

Total Files

8

Last publish

Collaborators

  • greladesign