Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ckaznable/shared-tick
Run function in same setInterval
https://github.com/ckaznable/shared-tick
no-dependencies setinterval setinterval-timer tick typescript typescript-library vanilla-js
Last synced: about 1 month ago
JSON representation
Run function in same setInterval
- Host: GitHub
- URL: https://github.com/ckaznable/shared-tick
- Owner: ckaznable
- License: mit
- Created: 2023-01-07T05:09:04.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T02:35:13.000Z (almost 2 years ago)
- Last Synced: 2024-10-06T14:35:17.254Z (3 months ago)
- Topics: no-dependencies, setinterval, setinterval-timer, tick, typescript, typescript-library, vanilla-js
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/shared-tick
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![npm version](https://img.shields.io/npm/v/shared-tick?color=g)](https://www.npmjs.com/package/shared-tick)
# SharedTick.js
Run function in same `setInterval`
## Installation
use npm
```
npm install shared-tick --save
```use yarn
```
yarn add shared-tick
```use pnpm
```
pnpm install shared-tick
```## Framework
|Framework|Repository|
|:---|:---:|
| Vue 3 | [vue-use-shared-tick](https://github.com/ckaznable/vue-use-shared-tick) |
| React | [react-use-shared-tick](https://github.com/ckaznable/react-use-shared-tick) |## Usage
run function per second
```js
import { runTick } from 'shared-tick'const onTick = () => {
// do something per second
}runTick({onTick})
```run function per 3 second
```js
import { runTick } from 'shared-tick'const onTick = () => {
// do something per second
}runTick({onTick, tick: 3000})
```stop runner
```js
import { stopTickRunner, runTick } from "shared-tick"const id = runTick({...})
stopTickRunner(id)
```restart runner
```js
import { stopTickRunner, startTickRunner, runTick } from "shared-tick"const id = runTick({...})
stopTickRunner(id)// restart runner after 5sec
setTimeout(() => startTickRunner(id), 5000)
```## API
### runTick
```js
runTick(config)
```### Parameter
| Field | Type | Default | Requirement |Description |
|---|:---:|---|---|---|
| tick | `number` | `1000` | `No` | Running interval time |
| id |`symbol`| `Symbol()` | `No` | Identifying objects |
| onTick | `(t: number) => void` | `No` | `Yes` | Runner function, The first parameter is the current timestamp |### stopTickRunner
```js
stopTickRunner(id: symbol)
```### stopTickRunner
```js
startTickRunner(id: symbol)
```## License
The files included in this repository are licensed under the MIT license.