GithubHelp home page GithubHelp logo

clock's Introduction

clock2listen

Install

npm i clock2listen

Usage

import ClockTimer from 'clock2listen'

let timer = null

timer = new ClockTimer(Date.now())

// Triggered every second
const cancelId = timer.addListen(() => {
    console.log('1s')
})

// Triggered at a specific time
timer.addClock({
    time: Date.now() + 1000 * 6,
    listen: () => {
        console.log('6s')
        timer.cancelListen(cancelId) // cancel listen
    },
    id: 'six'
})

constructor(time: number)

The constructor is used to create an instance of ClockTimer.

Parameters:

  • time (number): The initial server time in milliseconds.

addClock(clock: {time: number; listen: () => void; id?: string;}): string

Adds a clock task to be executed at a specified time.

Parameters:

  • clock (object): An object containing the following properties.
    • time (number): The time at which the task should be executed in milliseconds.
    • listen (function): The callback function to be called when the task is executed.
    • id (string): A unique identifier for the task. If not provided, a unique identifier will be generated.

Returns:

  • id(string): The unique identifier of the added clock task.

cancelClock(id: string)

Cancels a clock task with the specified identifier.

Parameters:

  • id (string): The unique identifier of the task to cancel.

addListen(listen: (time: number) => void): string

Adds a listener to be triggered on every second update.

Parameters:

  • listen (function): The callback function to be called on every second update.

Returns:

  • id(string): The unique identifier of the added listener.

cancelListen(id: string)

Cancels a listener with the specified identifier.

Parameters:

  • id (string): The unique identifier of the listener to cancel.

destroy()

Destroys the ClockTimer instance, stopping the timer.

clock's People

Contributors

sxy15 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.