Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @georapbox/timer with all npm packages installed. Try it out:

var timer = require("@georapbox/timer")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@georapbox/timer v1.1.2

Minimal javascript library to create and manage timers

npm version Build Status dependencies Status devDependencies Status License: MIT

Timer

Minimal javascript library to create and manage timers

NOTE: Depends on window.requestAnimationFrame. If your environment does not support it, you can polyfill.

Install

$ npm install @georapbox/timer --save

API

new Timer([duration], [callback])

Creates a Timer instance.

ParamTypeDescription
[duration]NumberThe timer's duration (ms). If left undefined or 0 the timer counts up instead of down.
[callback]functionFunction to be executed while timer is running. The Timer instance is passed by as parameter.

timer.time() ⇒ Object

Get the remaining and elapsed time.
If no duration is specified during initialization, the remaining time will always be 0.

Kind: instance method of Timer
Returns: Object - An object that contains the remaining and the elapsed time in milliseconds.

timer.start([shouldReset]) ⇒ Timer

Start the timer.
If the timer instance has been already started, the timer will just resume.

Kind: instance method of Timer
Returns: Timer - The Timer instance.

ParamTypeDescription
[shouldReset]BooleanIf set to true, the timer will reset to initial specified duration.

timer.stop() ⇒ Timer

Stop/Pause the timer.

Kind: instance method of Timer
Returns: Timer - The Timer instance.

timer.reset([shouldStop]) ⇒ Timer

Resets the timer to initial specified duration.

Kind: instance method of Timer
Returns: Timer - The Timer instance.

ParamTypeDescription
[shouldStop]BooleanIf set to true, the timer will be forced to stop; otherwise will reset and continue running.

timer.isRunning() ⇒ Boolean

Check (at any time) if the timer is running or not.

Kind: instance method of Timer
Returns: Boolean - True if the timer is running; otherwise false.

Test

$ npm run test

License

The MIT License (MIT)

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free