Skip to content

JavaScript alternative to setTimeout offering time manipulation features

License

Notifications You must be signed in to change notification settings

jamro/clockwarp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Param Type Description
dt Number amount 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

Param Type Default Description
callback function A function to be executed after the timer expires.
duration Number The time, in milliseconds that the timer should wait before the specified function or code is executed.
[repeat] Boolean false internal. 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

Param Type Description
callback function A function to be executed every duration milliseconds. The first execution happens after delay milliseconds.
duration Number The 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

Param Type Description
event Object event object to be canceled

About

JavaScript alternative to setTimeout offering time manipulation features

Topics

Resources

License

Stars

Watchers

Forks