pax_global_header00006660000000000000000000000064124741042760014521gustar00rootroot0000000000000052 comment=19f9fd95782d372f2d0be6d5c60d60f8176d7ef8 throttle-1.0.0/000077500000000000000000000000001247410427600133645ustar00rootroot00000000000000throttle-1.0.0/.gitignore000066400000000000000000000000361247410427600153530ustar00rootroot00000000000000components build node_modules throttle-1.0.0/History.md000066400000000000000000000004631247410427600153520ustar00rootroot00000000000000 1.0.0 / 2015-02-27 ================== - Internal refactor - Removed `max` argument - Context for invocation is cached/cleared properly - More tests 0.0.2 / 2013-03-26 ================== - Cache the return value - Don't use `setTimeout()` 0.0.1 / 2013-03-26 ================== - Initial release throttle-1.0.0/Makefile000066400000000000000000000004311247410427600150220ustar00rootroot00000000000000 build: components index.js @component build --dev components: component.json @component install --dev clean: rm -fr build components template.js test: node_modules @./node_modules/mocha/bin/mocha \ --reporter spec node_modules: package.json @npm install .PHONY: clean throttle-1.0.0/Readme.md000066400000000000000000000011431247410427600151020ustar00rootroot00000000000000 # throttle Throttle a function ## Installation $ component install component/throttle ## Example var throttle = require('throttle'); window.onresize = throttle(resize, 200); function resize(e) { console.log('height', window.innerHeight); console.log('width', window.innerWidth); } ## API ### throttle(fn, wait) Creates a function that will call `fn` at most once every `wait` milliseconds. Supports leading and trailing invocation. `fn` will receive last context (`this`) and last arguments passed to a throttled wrapper before `fn` was invoked. ## License MIT throttle-1.0.0/component.json000066400000000000000000000003531247410427600162620ustar00rootroot00000000000000{ "name": "throttle", "repo": "component/throttle", "description": "Throttle a function", "version": "0.0.2", "keywords": [], "dependencies": {}, "development": {}, "license": "MIT", "scripts": [ "index.js" ] } throttle-1.0.0/example.js000066400000000000000000000003351247410427600153560ustar00rootroot00000000000000 var throttle = require('./'); function onprogress(n) { console.log('progress %s%', n); } onprogress = throttle(onprogress, 500); var n = 0; setInterval(function(){ if (n >= 100) return; onprogress(n++); }, 50); throttle-1.0.0/index.js000066400000000000000000000014641247410427600150360ustar00rootroot00000000000000module.exports = throttle; /** * Returns a new function that, when invoked, invokes `func` at most once per `wait` milliseconds. * * @param {Function} func Function to wrap. * @param {Number} wait Number of milliseconds that must elapse between `func` invocations. * @return {Function} A new function that wraps the `func` function passed in. */ function throttle (func, wait) { var ctx, args, rtn, timeoutID; // caching var last = 0; return function throttled () { ctx = this; args = arguments; var delta = new Date() - last; if (!timeoutID) if (delta >= wait) call(); else timeoutID = setTimeout(call, wait - delta); return rtn; }; function call () { timeoutID = 0; last = +new Date(); rtn = func.apply(ctx, args); ctx = null; args = null; } } throttle-1.0.0/package.json000066400000000000000000000006341247410427600156550ustar00rootroot00000000000000{ "name": "throttleit", "description": "Throttle a function", "version": "1.0.0", "keywords": [], "repository": { "type": "git", "url": "git://github.com/component/throttle.git" }, "devDependencies": { "mocha": "^1.18.0" }, "license": "MIT", "component": { "scripts": { "throttle/index.js": "index.js" } }, "scripts": { "test": "mocha --reporter spec" } } throttle-1.0.0/test.js000066400000000000000000000030451247410427600147030ustar00rootroot00000000000000var assert = require('assert'); var throttle = require('./'); describe('throttle', function(){ function counter() { function count(){ count.invoked++; } count.invoked = 0; return count; } it('should throttle a function', function(done){ var count = counter(); var wait = 100; var total = 500; var fn = throttle(count, wait); var interval = setInterval(fn, 20); setTimeout(function(){ clearInterval(interval); assert(count.invoked === (total / wait)); done(); }, total + 5); }); it('should call the function last time', function(done){ var count = counter(); var wait = 100; var fn = throttle(count, wait); fn(); fn(); assert(count.invoked === 1); setTimeout(function(){ assert(count.invoked === 2); done(); }, wait + 5); }); it('should pass last context', function(done){ var wait = 100; var ctx; var fn = throttle(logctx, wait); var foo = {}; var bar = {}; fn.call(foo); fn.call(bar); assert(ctx === foo); setTimeout(function(){ assert(ctx === bar); done(); }, wait + 5); function logctx() { ctx = this; } }); it('should pass last arguments', function(done){ var wait = 100; var args; var fn = throttle(logargs, wait); fn.call(null, 1); fn.call(null, 2); assert(args && args[0] === 1); setTimeout(function(){ assert(args && args[0] === 2); done(); }, wait + 5); function logargs() { args = arguments; } }); });