GithubHelp home page GithubHelp logo

jamro / clockwarp Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 59 KB

JavaScript alternative to setTimeout offering time manipulation features

License: MIT License

JavaScript 100.00%
events javascript settimeout timeline

clockwarp's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.