Skip to content

Files

Latest commit

2c5dfc7 · Feb 3, 2025

History

History
90 lines (62 loc) · 1.31 KB

README.md

File metadata and controls

90 lines (62 loc) · 1.31 KB

Timers plugin for litecanvas

Helpers to manage timers in litecanvas games.

Install

NPM: npm i @litecanvas/plugin-timers

CDN: https://unpkg.com/@litecanvas/plugin-timers/dist/dist.js

Usage

import litecanvas from "litecanvas"
import pluginTimers from "@litecanvas/plugin-timers"

litecanvas({
  loop: { init },
})

use(pluginTimers) // load the plugin

function init() {
  wait(5, () => {
    // this function will be executed after 5 seconds
  })

  loop(2, () => {
    // this function will be executed every 2 seconds
  })

  repeat(10, 2, () => {
    // this function will be executed only 10 times every 2 seconds
  })
}

Other features

Available for any kind of timer

Stop a timer:

const t = wait(5, () => {
  // ...
})

t.stop() // cancel the timer

Pause a timer:

const t = wait(5, () => {
  // ...
})

t.pause() // pause the timer

t.resume() // resume a paused timer

t.running // true if the timer is not paused

Get/set the remaining time:

const t = wait(5, () => {
  // ...
})

t.remaining += 10 // add 10 seconds

Get all active timers:

...
litecanvas()

use(pluginTimers, {
  exposeTimers: true // enable that settings
})

// now you can use the TIMERS variable
TIMERS.forEach((t) => {
  // ...
})