Skip to content

bastienrobert/clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clock

Simple clock to get current time using delta-time

Developped to be used with really old browsers and with or without module bundler.
Fast & ~550B gzipped

Install

npm install @bastienrobert/clock

Usage

import Clock from '@bastienrobert/clock'

const clock = new Clock()

requestAnimationFrame(() => {
  const delta = clock.update()
  console.log(delta, clock.currentTime)
})

API

  • clock.currentTime: elapsed time since the clock has been start
  • clock.state: current state of the clock (enum State is exported too for Typescript)
  • clock.now: latest Date.now() recorded by the clock
  • clock.last: latest Date.now() recorded before clock.now

Methods

Method Description
Start Set the current time to 0 and start the clock, returns state
Resume Resume the current time after STOP (the current time is computed with a delta-time, the elapsed time between stop and resume is included), returns state
Stop Stop the current time (without reset of the current time), returns state
Update Update the current time, need to be in your loop, returns elapsed (delta) time between last call