function-tools
Advanced tools
Comparing version 0.0.1 to 0.1.0
"use strict"; | ||
/** | ||
Useful higher-order functions | ||
@module | ||
@alias f | ||
*/ | ||
exports.throttle = throttle; | ||
/** | ||
Guarantees a function a specified `restPeriod` in between invocations. | ||
@param {Function} - the function to throttle | ||
@param {Object} - options | ||
@returns {Function} | ||
@alias module:function-tools.throttle | ||
*/ | ||
function throttle(f, options){ | ||
var resting = false; | ||
var args = null; | ||
var y = 0 | ||
var timer = null; | ||
var lastRun = 0; | ||
return function throttled(){ | ||
args = arguments; | ||
// console.log("run %d, n=%d", ++y, args[0]); | ||
if (!resting){ | ||
f.apply(this, args); | ||
resting = true; | ||
setTimeout(function(){ | ||
resting = false; | ||
if (args) throttled.apply(null, args); | ||
args = null | ||
}, options.interval || 1000); | ||
clearTimeout(timer); | ||
var fArgs = arguments; | ||
var timeSinceLastRun = Date.now() - lastRun; | ||
if (timeSinceLastRun > options.restPeriod){ | ||
f.apply(f, fArgs); | ||
lastRun = Date.now(); | ||
} else { | ||
timer = setTimeout(function(){ | ||
f.apply(f, fArgs); | ||
lastRun = Date.now(); | ||
}, options.restPeriod - timeSinceLastRun); | ||
} | ||
} | ||
} | ||
function printNumber(n){ | ||
console.log("%s: %s", new Date().toLocaleTimeString(), n); | ||
} | ||
var throttledPrint = throttle(printNumber, { interval: 100 }); | ||
var i = 0; | ||
var interval = setInterval(function(){ | ||
throttledPrint(++i); | ||
if (i === 27) clearInterval(interval); | ||
}, 100); |
{ | ||
"name": "function-tools", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Useful higher-order functions", | ||
@@ -19,3 +19,7 @@ "repository": "https://github.com/75lb/function-tools.git", | ||
"docs": "jsdoc2md -t jsdoc2md/README.hbs lib/*.js > README.md; echo" | ||
}, | ||
"devDependencies": { | ||
"jsdoc-to-markdown": "^0.1.9", | ||
"tape": "^2.13.3" | ||
} | ||
} |
@@ -6,2 +6,42 @@ [![view on npm](http://img.shields.io/npm/v/function-tools.svg)](https://www.npmjs.org/package/function-tools) | ||
#function-tools | ||
Useful higher-order functions | ||
**Contents** | ||
* [throttle(f, options)](#module_function-tools.throttle) | ||
<a name="module_function-tools.throttle"></a> | ||
###f.throttle(f, options) | ||
Guarantees a function a specified `restPeriod` in between invocations. | ||
- f `function` the function to throttle | ||
- options `Object` options | ||
**Returns**: `function` | ||
var test = require("tape"); | ||
var lib = require("../"); | ||
var f = require("../"); | ||
test("first", function(t){ | ||
t.plan(3); | ||
function testFunc(){ | ||
t.pass(); | ||
} | ||
var throttled = f.throttle(testFunc, { restPeriod: 200 }); | ||
var i = 0; | ||
var interval = setInterval(function(){ | ||
throttled(++i); | ||
if (i === 30) clearInterval(interval); | ||
}, 10); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4082
10
62
47
2