1.0.3 • Published 1 year ago

clockwarp v1.0.3

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

npm version

ClockWrap

ClockWarp is an alternative to the built-in setTimeout and setInterval functions. This library provides time manipulation capabilities that allow you to adjust the timing of events in your code. ClockWarp has a time multiplier which can be used to make all events run faster or slower, and it also has a fastForward method that can be used to move time forward.

Installation

To install the latest version on npm locally and save it in your package's package.json file:

npm i --save clockwarp

Example

const clock = new ClockWrap()

clock.setTimeout(() => console.log("Hello World"), 5000);
clock.setTimeout(() => console.log("This is another message"), 5500);

clock.fastForward(5000)
// console output: Hello World

// wait another 500ms
await new Promise((done) => setTimeout(done, 500));
// console output: This is another message

API Documentation

ClockWarp

Handle events execution on the timeline allowing time manipulation

Kind: global class

new ClockWarp()

Constructor

clockWarp.timeScale

Time multiplier for the clock. For example, when setting the value to 2, all events are going to be executed twice faster than usual.

Kind: instance property of ClockWarp

clockWarp.now

equivalent of performance.now(). The value reflects all time manipulations by fastForward or timeScale

Kind: instance property of ClockWarp

clockWarp.fastForward(dt)

Move time of the clock by specified duration. This operation will execute all events scheduled for that duration

Kind: instance method of ClockWarp

ParamTypeDescription
dtNumberamount of miliseconds to fast forward

clockWarp.setTimeout(callback, duration, repeat) ⇒ Object

sets a timer which executes a function once the timer expires.

Kind: instance method of ClockWarp
Returns: Object - scheduled event object

ParamTypeDefaultDescription
callbackfunctionA function to be executed after the timer expires.
durationNumberThe time, in milliseconds that the timer should wait before the specified function or code is executed.
repeatBooleanfalseinternal. cause function to work as setInterval

clockWarp.setInterval(callback, duration) ⇒ Object

repeatedly calls a function, with a fixed time delay between each call.

Kind: instance method of ClockWarp
Returns: Object - scheduled event object

ParamTypeDescription
callbackfunctionA function to be executed every duration milliseconds. The first execution happens after delay milliseconds.
durationNumberThe time, in milliseconds that the timer should wait before the specified function or code is executed.

clockWarp.clear(event)

cancels an event previously established by calling setTimeout or setInterval

Kind: instance method of ClockWarp

ParamTypeDescription
eventObjectevent object to be canceled
1.0.3

1 year ago

1.0.2

1 year ago

1.0.0

1 year ago