Lete114 / worker-tick

Make setTimeout() and setInterval() timers work stably in background tabs (unfocused browser tabs)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

worker-tick

npm version npm downloads bundle JSDocs License

Make setTimeout() and setInterval() timers work stably in background tabs (unfocused browser tabs)

Browsers reduce the frequency of setTimeout() and setInterval() when a browser tab is inactive (e.g., loses focus, switches to another tab, or switches to another application) for performance optimization reasons requestAnimationFrame() does the same thing

Install

Using npm:

npm install worker-tick

Usage

Refer to development in the repository

import workerTick from 'worker-tick'

- window.setInterval(console.log, 1000, 1, 2, 3)
+ workerTick.setInterval(console.log, 1000, 1, 2, 3)
// => Printouts per second: 1 2 3

// clear
const timerId = workerTick.setInterval(console.log, 1000, 1, 2, 3)
workerTick.clearInterval(timerId)

// setTimeout() Same as above

License

MIT License © 2024-PRESENT Lete114

About

Make setTimeout() and setInterval() timers work stably in background tabs (unfocused browser tabs)

License:MIT License


Languages

Language:TypeScript 79.9%Language:HTML 17.2%Language:JavaScript 2.9%