method-throttle
v1.1.0
Published
throttles a method for a specific time
Downloads
1,396
Maintainers
Readme
method-throttle
throttles a method for a specific time
Installation
npm install --save method-throttle
Usage
Delay your methods execution by defining an interval of idle time.
var throttle = require( 'method-throttle' );
var method = throttle(function ( a, b, c ){
// your code as usually
}, 1000 );
method( null, null, 1 );
method( null, null, 1 );
method( null, null, 1 );
method( null, null, 1 );
setTimeout( method, 1500 );
// function provided into throttle would run only twice!
Example use case:
Imagine that you want to register how many times an user scrolls a page.
If you place an event listener on window.scroll
, you would have a ton of data
since almost all browsers fire those events whenever page is modified / rendered.
var timesScrolled = 0;
window.on( 'scroll', function () {
timesScrolled++;
});
That would not be a bottleneck, but for most advanced use cases, you would be some how pissed off.
With throttle you can limit how many times, within a provided interval of time, a method could run:
var throttle = require( 'method-throttle' );
var timesScrolled = 0;
window.on( 'scroll', throttle(function () {
timesScrolled++;
}, 1000 ));
Now, if you were always scrolling, you would get at a maximum of 60 count!!
:mokey-face:
Testing
We use mocha
, chai
, bluebird
and sinon
for this test suite!
npm test