npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

advanced-timer

v2.0.1

Published

Advanced timer with pause/resume/reset capabilites, conditional cycle execution, live timer delay manipullation, and much more.

Downloads

54

Readme

advanced-timer

NoDependencies Browser Node

Advanced timer with pause/resume/reset capabilites, conditional cycle execution, onComplete tasks scheduling, live timer delay manipullation, and much more.

  import {Timer} from 'advanced-timer.js';
  // run myFunc 50 times every 1000ms
  let myTimer = new Timer( 1000 ).action( myFunc ).repeat( 50 ).start();

Some features:

  • Chainable constructor
  • Able to Pause/resume/reset
  • Live delay change ( Timer.delay() I)
  • Conditional cycles ( Timer.if() )
  • On complete callbacks ( Timer.done() )
  • Live iterations count change ( Timer.repeat() )
  • Sync lost detection ( Timer.inSync )
  • Cycle timestamp, deltaTime...
  • Node & Browser compatible (Node Module, ES6 module , Script Inlcude)
  • Tiny and Zero dependencies

See it in action here.

Usage example

Create a timer that executes 50 times the callback every 1000ms (only when timer cycle remains in sync), and execute an onComplete callback when done.

  let myTimer = new Timer( 1000 )
      .action( t=> console.log(t.currentCycle) )
      .if( t=> t.inSync )
      .repeat( 50 )
      .done( t=> console.log('completed!') )
      .start();

Package distribution :

The following distribution channels are available to download and install the package :

  • NPM : Install the package using the NPM network :
$ npm install advanced-timer --save
  • GIT : Clone the repository from Github (or download the latest ZIP package release here )
 $ git clone https://github.com/colxi/advanced-timer.git
  • CDN : Include this library in the HEAD of your HTML :
<script src='https://colxi.info/advanced-timer/src/main.js'></script>

Import/Include the library

This library can be imported in Node using ...

let Timer = require('advanced-timer'); 

Imported (ES6 module) in the browser using ...

import {Timer} from './library-path/src/advanced-timer.js';

Or included in the browser adding in your HTML head ...

<script src='./library-path/src/main.js'></script>
<!-- window.Timer, is created -->

Constructor

Create a new timer instance using:

  let myTimer = new Timer( milliseconds );
  • milliseconds : Integer representing the delay betwen cycles.

IMPORTANT: The Constructor deesn't start the Timer, it only sets and returns the new timer Instance. You mus call the start , and assign an action method, in order to start it .

Timer prototype methods

Timer.prototype.action

Sets the function to be executed in each timer cycle. Is required to be able to start the timer.

    Timer.prototype.action( callback );
  • callback: Function to execute in each timer cycle. The function execution context will be binded to the timer (unless if declared as arrow functon), and will recieve a reference to the timer objct as the first argument.

Note: Action callback can be changed any time.

Timer.prototype.start

Starts the timer. If timer is paused, will be resumed.

   Timer.prototype.start();

If the timer is completed (status 3) must be reseted first in order to use it again.

Timer.prototype.stop

Stops the timer and resets all its values.

    Timer.prototype.stop();

Timer.prototype.pause

Pauses the timer.

    myTimer.pause();

Timer.prototype.resume

Resumes the timer.

    Timer.prototype.resume();

Only a paused timer (status 2) can be resumed.

Timer.prototype.repeat

Sets the ammount of cycles before the timer stops. By default, if repeat is not set, the timer will run infinitelly.

    Timer.prototype.repeat( times );
  • times: Positive integer (or Boolean) representing the ammount of cycles to execute. If false is set, a single cycle wil be executed. If set to true will run infinitelly.

Note: Can be changed any time.

Timer.prototype.delay

Sets the delay betwen each timer cycle

    Timer.prototype.delay( milliseconds );
  • milliseconds: Integer representing ammount of milliseconds betwen each cycle callback execution.

Note: Delay time can be changed any time.

Timer.prototype.reset

Resets the timer and all its atributes. It also sets the timer status to stopped, unless the timer was reseted in running state in such case, it starts running again automatically after the reset.

    Timer.prototype.reset();

Note: Reset allows to re-initialize a completed timer.

Timer.prototype.if

Sets a callback to be executed before each cycle call. If returns true the cycle callback will be executed, but ommited if returns false.

    Timer.prototype.if( callback );
  • callback: Function to be executed. Function recieves the timer reference as the first argument¡

Note : Conditional callback can be disabled any time, by providing false as argument: myTimer.if( false )

Timer.prototype.done

Sets the callback to be executed when the timer reaches the las scheduled cycle.

    Timer.prototype.done( callback );
  • callback: Function to be executed. Function recieves the timer reference as the first argument¡

Timer.prototype.destroy

Destroys the timer and all its internal properties. Timer becomes unusable after being destroyed. Lets the Garbage Collector discard the instance reference.

    Timer.prototype.destroy();

Timer.prototype.setSyncThreshold

Sets the out of sync threshold in ms. If a cycle deltaTime exceeds the timer freqency (timerDelay) by an ammount of miliseconds higher than the set threshold, the inSync flag, will become false.

    Timer.prototype.setSyncThreshold( ms );
  • ms: Positive integer representing the ammountof miliseconds.

Timer prototype properties

  • Timer.prototype.statusCode : Integer representing the timer status (-1,0,1,2,3)
  • Timer.prototype.status : String representing the status of the timer ( destroyed, stopped, running, paused, completed)
  • Timer.prototype.inSync: Boolean. When cycle callback can't be executed respecting the provided delay + syncThreshold, ìnSync becomes false.
  • Timer.prototype.timerDelay: Integer representing the timer frequency.
  • Timer.prototype.timestamp: Integer represemting the current timestamp.
  • Timer.prototype.currentCycle: Integer. Cycle counter.
  • Timer.prototype.cycleLimit: Integer representing the total ammount of cyles to be executed before timer stops.
  • Timer.prototype.cycleTimestamp : Integer representing te timestamp corresponding to the current cycle starting time, relative to the timer start.
  • Timer.prototype.cycleDeltatime: Integer representing the ammount of milliseconds, since the last cycle.If the cycle is ìnSync must match (or almost) with the provided delayvalue.
  • Timer.prototype.syncThreshold : Integer representing the ammount of miliseconds. (default value 20ms)

Licence

MIT