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

dynamic-timer

v1.0.4

Published

Schedule execution of a multi-time callback after delay milliseconds, automatic, intelligence and without bothering, the delay is calculated from different algorithms, e.g.: Lucas Sequence, Fibonacci Sequence, DaYan Series and Arithmetic Procession.

Downloads

9

Readme

dynamic-timer NPM version Build Status

Schedule execution of a multi-time callback after delay milliseconds, automatic, intelligence and without bothering, the delay is calculated from different algorithms, e.g.: Lucas Sequence, Fibonacci Sequence, DaYan Series and Arithmetic Procession.

So, the next delay will be generated by the specific strategy, but not a fixed one. This will take a high performance on long-polling, handshake, ping-pong, reconnect, task worker or something else.

Installation

npm install dynamic-timer

Guide

Usage

var dynamicTimer = require('dynamic-timer');
var dynamicTimeout = dynamicTimer([options]);

options are including:

  • seed The cardinal number of delay, 1000(milliseconds) as default.
  • delay The first callback will be called in exactly milliseconds, 1000(milliseconds) as default.
  • strategy Calculate delay milliseconds by strategy, see more information from Strategy.
  • maxAttempts The maximize attempts, the Timer locks maxAttempts for different strategy, even when you set the maxAttempts, it can not greater than bellows:
    • procession 5000
    • dayan 141
    • fibonacci 20
    • lucas 19
  • maxDelay The maximize delay.
  • overrun When the attempts greater than maxAttempts or next delay greater than maxDelay means Timer is overrunning, this property should be:
    • stop Stop the timer immediately, equals dynamicTimeout.state.STOP
    • reset Reset the Timer to initial state and start again, equals dynamicTimeout.state.RESET
    • overload Keep running, but all the next delays will be set to maxDelay, equals dynamicTimeout.state.OVERLOAD
  • autostart Automatic start Timer, default as false, if the property was set to true, there is no necessary to do dynamicTimeout.start

Strategy

Timer supports four strategies:

  • procession Arithmetic Procession, equals dynamicTimeout.strategy.PROCESSION, the sequence increases like:

    1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21...

    It is increasing placid, so when you set the seed as 1000, the Timer will trigger after 1s, 3s, 5s, 7s, 11s... not insipid 1s, 1s, 1s....

  • dayan DaYan Series, equals dynamicTimeout.strategy.DAYAN, the sequence increases like:

    0, 2, 4, 8, 12, 18, 24, 32, 40, 50...

    It is increasing placid too, but speedier then Arithmetic Procession, I like this algorithm most, so using it as default value for strategy.

  • fibonacci Fibonacci Sequence, equals dynamicTimeout.strategy.FIBONACCI, the sequence increase like:

    1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377...

    It is increasing speedy, we can use this strategy to deal with some non-concurrent job.

  • lucas Lucas Sequence, equals dynamicTimeout.strategy.LUCAS, the sequence increase like:

    1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, 322, 521...

    Lucas is similar to Fibonacci but increases speedier.

Notes: If you are interesting in these algorithms, you can take a test with the example/algorithm.js.

Events

tick

This event is triggered after delay milliseconds, do your job in the callback:

dynamicTimeout.on('tick', function(){
  console.log('#' + dynamicTimeout.attempts, 'next after', dynamicTimeout.delay);
})

toll

This event is triggered after state has been changed.

dynamicTimeout.on('toll', function(state){
  console.log('current state:', state);
})

Methods

start

Start the Timer, when autostart set to true, this event triggered nothing.

dynamicTimeout.start();

stop

Stop the Timer, when current state of Timer was STOP, this event triggered nothing.

dynamicTimeout.stop();

pause

Stop the Timer, when current state of Timer was not RUNNING, this event triggered nothing.

dynamicTimeout.pause();

resume

Resume the Timer, when current state of Timer was not PAUSE, this event triggered nothing.

dynamicTimeout.resume();

reset

Reset the Timer to initial status.

// stop timer.
dynamicTimeout.reset(true);

The argument indicates whether stop the timer or not, if it was not passed or set to false, just reset Timer and tick again from initial status.

Properties

attempts

The attempts Timer has run.

delay

The next delay(milliseconds).

state

Current state of Timer, including:

  • dynamicTimeout.state.PAUSE, equals pause,
  • dynamicTimeout.state.RESUME, equals resume,
  • dynamicTimeout.state.RESET, equals reset,
  • dynamicTimeout.state.RUNNING, equals running,
  • dynamicTimeout.state.STOP, equals stop

Example

var dynamicTimer = require('../'),
  util = require('util');

var dynamicTimeout = dynamicTimer({
  seed:1000,
  delay:1000,
  strategy:dynamicTimer.strategy.PROCESSION,
  maxAttempts:10,
  maxDelay:4000,
  overrun:dynamicTimer.state.STOP, // stop, reset, overload
  autostart:false
});
dynamicTimeout.on('tick', function (o) {
  util.log('#' + dynamicTimeout.attempts + ', next after ' + dynamicTimeout.delay + ' ms');
});
dynamicTimeout.on('toll', function (state) {
  util.log(state);
});
util.log('start timer');
dynamicTimeout.start();


// test pause, resume and stop.
setTimeout(function () {
  dynamicTimeout.pause();
}, 3000);

setTimeout(function () {
  dynamicTimeout.resume();
}, 10000);
setTimeout(function () {
  dynamicTimeout.resume();
}, 6000);

setTimeout(function () {
  dynamicTimeout.stop();
}, 31000);
setTimeout(function () {
  dynamicTimeout.stop();
}, 32000);

See more examples under example and test folders.

Test

npm test

TODO

  • [ ] Test cases.
  • [ ] Typo bug fixing.

License

Copyright 2014 Tjatse

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.