All Projects → chrisguttandin → Worker Timers

chrisguttandin / Worker Timers

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Worker Timers

Musictheory
Universal music theory library for iOS, iPadOS, macOS, tvOS and watchOS in Swift
Stars: ✭ 262 (+61.73%)
Mutual labels:  interval
Nim Lapper
fast easy interval overlapping for nim-lang
Stars: ✭ 23 (-85.8%)
Mutual labels:  interval
Brein Time Utilities
Library which contains several time-dependent data and index structures (e.g., IntervalTree, BucketTimeSeries), as well as algorithms.
Stars: ✭ 94 (-41.98%)
Mutual labels:  interval
Net
Android上强大的网络请求
Stars: ✭ 344 (+112.35%)
Mutual labels:  interval
React Native Gesture Password
A gesture password component for React Native. It supports both iOS and Android since it's written in pure JavaScript.
Stars: ✭ 530 (+227.16%)
Mutual labels:  interval
Interval Arithmetic
An implementation of an algebraically closed interval system of the extended real number set
Stars: ✭ 60 (-62.96%)
Mutual labels:  interval
cron-schedule
A zero-dependency cron parser and scheduler for Node.js, Deno and the browser.
Stars: ✭ 28 (-82.72%)
Mutual labels:  interval
Instruments
Collecting metrics over discrete time intervals
Stars: ✭ 111 (-31.48%)
Mutual labels:  interval
Period
PHP's time range API
Stars: ✭ 616 (+280.25%)
Mutual labels:  interval
Steamcmd Autoupdate Any Gameserver
Windows SteamCMD to autoupdate and install any game server steam cmd settings configurable lots of useful features. This batch script will keep your game servers automaticly updated updating intervals announce the server is shutting down for updates etc all configurable.
Stars: ✭ 77 (-52.47%)
Mutual labels:  interval
Human Interval
Human readable time distances for javascript
Stars: ✭ 360 (+122.22%)
Mutual labels:  interval
Pg timetable
pg_timetable: Advanced scheduling for PostgreSQL
Stars: ✭ 382 (+135.8%)
Mutual labels:  interval
Iso8601
Ruby parser to work with ISO8601 dateTimes and durations — http://en.wikipedia.org/wiki/ISO_8601
Stars: ✭ 70 (-56.79%)
Mutual labels:  interval
Time4j
Advanced date, time and interval library for Java with sun/moon-astronomy and calendars like Chinese, Coptic, Ethiopian, French Republican, Hebrew, Hijri, Historic Christian, Indian National, Japanese, Julian, Korean, Minguo, Persian, Thai, Vietnamese
Stars: ✭ 328 (+102.47%)
Mutual labels:  interval
Moment Timer
Timer plugin for Moment.js that allows creation of setInterval and setTimeout-like timers.
Stars: ✭ 98 (-39.51%)
Mutual labels:  interval
Portion
portion, a Python library providing data structure and operations for intervals.
Stars: ✭ 255 (+57.41%)
Mutual labels:  interval
Time
Type-safe time calculations in Kotlin, powered by generics.
Stars: ✭ 939 (+479.63%)
Mutual labels:  interval
Pyfts
An open source library for Fuzzy Time Series in Python
Stars: ✭ 154 (-4.94%)
Mutual labels:  interval
Spans
Spans is a pure Python implementation of PostgreSQL's range types.
Stars: ✭ 106 (-34.57%)
Mutual labels:  interval
Git Repo Watcher
A simple bash script to watch a git repository and pull upstream changes if needed.
Stars: ✭ 73 (-54.94%)
Mutual labels:  interval

logo

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:

import * as workerTimers from 'worker-timers';

The usage is exactly the same (despite of the error handling and the differentiation between intervals and timeouts) as with the corresponding functions on the global scope.

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

workerTimers.clearInterval(intervalId);

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

workerTimers.clearTimeout(timeoutId);

Error Handling

The native WindowTimers are very forgiving. Calling clearInterval() or clearTimeout() without a value or with an id which doesn't exist will just get ignored. In contrast to that workerTimers will throw an error when doing so.

// This will just return undefined.
window.clearTimeout('not-an-timeout-id');

// This will throw an error.
workerTimers.clearTimeout('not-an-timeout-id');

Differentiation between Intervals and Timeouts

Another difference between workerTimers and WindowTimers is that this package maintains two separate lists to store the ids of intervals and timeouts internally. WindowTimers do only have one list which allows intervals to be cancelled by calling clearTimeout() and the other way round. This is not possible with workerTimers. As mentioned above workerTimers will throw an error when provided with an unknown id.

const periodicWork = () => { };

// This will stop the interval.
const windowId = window.setInterval(periodicWork, 100);
window.clearTimeout(windowId);

// This will throw an error.
const workerId = workerTimers.setInterval(periodicWork, 100);
workerTimers.clearTimeout(workerId);

Server-Side Rendering

This package is intended to be used in the browser and requires the browser to have support for Web Workers. It does not contain any fallback which would allow it to run in another environment like Node.js which doesn't know about Web Workers. This is to prevent this package from silently failing in an unsupported browser. But it also means that it needs to be replaced when used in a web project which also supports server-side rendering. That should be easy, at least in theory, because each function has the exact same signature as its corresponding builtin function. But the configuration of a real-life project can of course be tricky. For a concrete example, please have a look at the worker-timers-ssr-example provided by @newyork-anthonyng. It shows the usage inside of a server-side rendered React app.

Angular (& zone.js)

If WorkerTimers are used inside of an Angular App and Zones are used to detect changes, the behavior of WorkerTimers can be confusing. Angular is using a Zone which is patching the native setInterval() and setTimeout() functions to get notified about the execution of their callback functions. But Angular (more specifically zone.js) is not aware of WorkerTimers and doesn't patch them. Therefore Angular needs to be notified manually about state changes that occur inside of a callback function which was scheduled with the help of WorkerTimers.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].