UNPKG

shared-timers

Version:

Unthrottleable timers that work across tabs.

37 lines (24 loc) 1.05 kB
# Shared Timers > [!WARNING] > Mostly does not work on mobile https://caniuse.com/sharedworkers Timers in [SharedWorker](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker), shared across browser tabs (within same origin). One timer per delay, many callbacks for each timer. ## Motivation * Keep timer callbacks in sync betweet tabs. * Escape the [throttling of window timers in inactive tabs](https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout#timeouts_in_inactive_tabs). ## Getting Started Install using your preferred package manager, for example NPM: ```shell npm install shared-timers ``` Use it in your project like this: ```typescript import { runTimer, clearTimer } from 'shared-timers' const firstCallbackId = runTimer(() => { // Something executed periodically from a newly created `setInterval`. }, 1000) const secondCallbackId = runTimer(() => { // Something executed periodically from the same `setInterval`. }, 1000) // Remove callback from timer/execution. clearTimer(secondCallbackId) ```