warped-time

warped-time: Control the progression of time npm version Github Stars Twitter URL

▶️ API Info redux-time Example Source

Simple Javascript library that provides an equivalent to Date.getTime() with variable progression speed.

It has a hook to receive timestamps and estimated latency, which it uses to stay in sync with a backend server’s time.

LIVE DEMO

yarn add warped-time    # ⏱

For more details and usage examples see docs on redux-time.

API

window.time = new WarpedTime(window.store, 1)
// takes: optional redux store, initial_speed, initial_timestamp

window.time.getActualTime()
> 1499014500
window.time.getWarpedTime()
> 1499014500

window.time.setSpeed(-1)   // make time start going backwards at -1x

window.time.getActualTime()
> 1499014501
window.time.getWarpedTime()
> 1499014499

window.time.setSpeed(0.01)  // make time progress at 0.01x its actual speed

window.time.getWarpedTime()
> 1499014499.01

const server_time = timestamp_from_server + rtlatency/2
window.time.setWarpedTime(server_time) // set time to the server time instantly

window.time.setWarpedTime(server_time, 1000) // sync to server time over 1000ms

Info

This library is useful for:

We use it for time-travel debugging and animation timing sync in our library redux-time.


MIT License Monadical SAS 2017 (we’re hiring!)