1.0.1 • Published 1 year ago

shared-tick v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

Usage

run function per second

import { runTick } from 'shared-tick'

const onTick = () => {
  // do something per second
}

runTick({onTick})

run function per 3 second

import { runTick } from 'shared-tick'

const onTick = () => {
  // do something per second
}

runTick({onTick, tick: 3000})

stop runner

import { stopTickRunner, runTick } from "shared-tick"

const id = runTick({...})
stopTickRunner(id)

restart runner

import { stopTickRunner, , startTickRunner, runTick } from "shared-tick"

const id = runTick({...})
stopTickRunner(id)

// restart runner after 5sec
setTimeout(() => startTickRunner(id), 5000)

API

runTick

runTick(config)

Parameter

FieldTypeDefaultRequirementDescription
ticknumber1000NoRunning interval time
idsymbolSymbol()NoIdentifying objects
onTick(t: number) => voidNoYesRunner function, The first parameter is the current timestamp

stopTickRunner

stopTickRunner(id: symbol)

stopTickRunner

startTickRunner(id: symbol)

License

The files included in this repository are licensed under the MIT license.