The window timer methods setTimeout and setInterval, recreated with Web Workers.
In "unfocussed" windows, the definition of which varies from browser to browser, the built-in setTimeout and setInterval methods are severely throttled as a performance optimisation. set-worker-timer acts as a work-around by executing window timers in the Worker thread which is not susceptible to this behaviour.
This package is an alternative to the popular worker-timers, opting to recreate timer functionality as closely as possible to the original API. This includes overlooked features such as indefinite arguments, and downright questionable ones such as string execution using eval(). User discretion is advised.
npm install set-worker-timer
import {
setWorkerTimeout,
setWorkerInterval,
clearWorkerTimer
} from 'set-worker-timer':
set-worker-timer attaches to the global scope so imports from any file always link back to the same class instantation.
const timeoutId = setWorkerTimeout(() => {
// execute callback after 100ms
}, 100);
const intervalId = setWorkerInterval(() => {
// execute callback every 100ms
}, 100);
clearWorkerTimer(timeoutId);
clearWorkerTimer(intervalId);
set-worker-timer uses one callback list and one Worker so either type of timer can be cleared with the same method.
Supply arguments to the timer to be passed as parameters to your callback function.
setWorkerTimeout((foo, bar) => {
// execute callback with parameters
}, 100, "foo", "bar")
Pass a string to be executed using eval().
setWorkerTimeout('() => {
// execute unsafe callback
}', 100);
-
- Remove esmodule build.
- Combine clear methods into clearWorkerTimer.
-
- Initial release.
This project is licensed under the MIT License - see the LICENSE.md file for details.