timetrickle

A time-based execution limiter

npm install timetrickle
14 downloads in the last week
55 downloads in the last month

Trickle

Trickle limits function executions based on time. It lets you define the number of executions per a certain amount of milliseconds.

This is particularly useful to obey an API provider's rate limit, for example.

Installation

Note: trickle is called "timetrickle" on npm because someone has occupied the name before I had a chance.

npm install timetrickle --save

or manually add it to your package.json

Example

var trickle = require('timetrickle');
    start = +Date.now();

// Helper function to show the time difference
function since (time) { return Math.round((time - start) / 1000) + 's'; }

// Limit once per second
var limit = trickle(1, 1000);

limit(function () {
    console.log('I am doing an API call here', since(+Date.now()));
});

limit(function () {
    console.log('I am doing another API call here', since(+Date.now()));
});

limit(function () {
    console.log('I am doing an API call here again', since(+Date.now()));
});

Will output:

I am doing an API call here 0s
I am doing an API call here again 1s
I am doing another API call here 2s
npm loves you