Coder Social home page Coder Social logo

tvanc / high-res-timeout Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 2.08 MB

A high-resolution, object-oriented replacement for setTimeout() and setInterval(). Good for animations, progress indicators, carousels, you name it.

JavaScript 100.00%
javascript js-library ecmascript6 timeout settimeout setinterval domhighrestimestamp requestanimationframe

high-res-timeout's Introduction

High Res Timeout

A high-resolution, object-oriented replacement for setTimeout() and setInterval(). Good for animations, progress indicators, carousels, you name it.

Installation

npm install --save-dev high-res-timeout

Usage

Simple timeout

import HighResTimeout from "high-res-timeout"
const timeout = new HighResTimeout(250)

Repeating timeout (setInterval)

import HighResTimeout from "high-res-timeout"
const interval = new HighResTimeout(250, true)

Lifecycle callbacks

As a Promise

timeout
    .start()
    .then(() => console.log("I am complete!"))
    .catch(() => console.log("I've been stopped!"))

Events

timeout.start()
    .on('complete', () => console.log('I am complete!'))
    .on('stop', () => console.log("I've stopped!"))
    .on('start', () => console.log("I've started!"))
    .on('reset', () => console.log("I've been reset!"))
    .on('tick', () => {
    	console.log(`I'm ${(timeout.progress * 100).toFixed(0))}% done`)
    });

Methods and properties

Instance methods

Method Chainable Description
start() Starts the timeout. Takes into account time elapsed between previous calls to start() and stop().
stop() Stops the timeout.
reset() Resets the timeout.
complete() Completes the timeout as if it had completed organically.
then() Attach a callback that will be called when the timeout completes. Returns a Promise.

Instance properties

Property Readable Writable Description
duration Get/set timeout duration.
progress Float between 0 and 1 indicating how much of the timeout's duration has elapsed.
running Whether the timeout is currently counting down.

Development

Use the following commands to perform code coverage after each change and to open a Browsersync tab to the coverage reports.

npm run watch
npm run serve

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.