├── .gitignore ├── test ├── mocha.opts ├── setup.js ├── basic_test.js └── standard_test.js ├── .travis.yml ├── HISTORY.md ├── package.json ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require test/setup 2 | -------------------------------------------------------------------------------- /test/setup.js: -------------------------------------------------------------------------------- 1 | global.expect = require('expect') 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - v4.1.0 4 | -------------------------------------------------------------------------------- /HISTORY.md: -------------------------------------------------------------------------------- 1 | ## v1.0.2 2 | > Sep 21, 2015 3 | 4 | - Add a [umdjs](https://github.com/umdjs/umd) wrapper for use outside CommonJS. 5 | 6 | ## v1.0.0 7 | > Sep 21, 2015 8 | 9 | - Initial version. 10 | -------------------------------------------------------------------------------- /test/basic_test.js: -------------------------------------------------------------------------------- 1 | describe('debounce()', function () { 2 | var debounce = require('../index') 3 | 4 | it('works', function (next) { 5 | function myFunction (argsList) { 6 | expect(argsList).toEqual([ [1, 'one'], [2, 'two'] ]) 7 | next() 8 | } 9 | 10 | var debounced = debounce(myFunction, 20) 11 | debounced(1, 'one') 12 | debounced(2, 'two') 13 | }) 14 | }) 15 | -------------------------------------------------------------------------------- /test/standard_test.js: -------------------------------------------------------------------------------- 1 | var standard = require('mocha-standard') 2 | 3 | describe('coding style', function () { 4 | this.timeout(5000) 5 | 6 | it('lib conforms to standard', standard.files([ 7 | '*.js', 8 | 'lib/**/*.js' 9 | ])) 10 | 11 | it('tests conform to standard', standard.files([ 12 | 'test/**/*.js' 13 | ], { 14 | global: [ 15 | 'describe', 'it', 'xdescribe', 'xit', 16 | 'before', 'beforeEach', 'after', 'afterEach', 17 | 'expect' 18 | ] 19 | })) 20 | }) 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "debounce-collect", 3 | "version": "1.0.2", 4 | "description": "Makes a function execute only once in a given interval.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/rstacruz/debounce-collect.git" 12 | }, 13 | "author": "Rico Sta. Cruz ", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/rstacruz/debounce-collect/issues" 17 | }, 18 | "homepage": "https://github.com/rstacruz/debounce-collect#readme", 19 | "devDependencies": { 20 | "expect": "^1.10.0", 21 | "mocha": "^2.3.3", 22 | "mocha-standard": "^1.0.0", 23 | "standard": "^5.3.1" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* global define */ 2 | void (function (root, factory) { 3 | if (typeof define === 'function' && define.amd) define(factory) 4 | else if (typeof exports === 'object') module.exports = factory() 5 | else root.debounceCollect = factory() 6 | }(this, function () { 7 | var now = Date.now || function now () { return new Date().getTime() } 8 | 9 | function debounce (func, wait, immediate) { 10 | var timer, context, timestamp, result 11 | var args = [] 12 | if (wait == null) wait = 100 13 | 14 | function onTimeout () { 15 | var elapsed = now() - timestamp 16 | 17 | if (elapsed < wait && elapsed > 0) { 18 | timer = setTimeout(onTimeout, wait - elapsed) 19 | } else { 20 | timer = null 21 | if (!immediate) { 22 | result = call() 23 | if (!timer) reset() 24 | } 25 | } 26 | } 27 | 28 | function call () { 29 | return func.call(context, args) 30 | } 31 | 32 | function reset () { 33 | context = null 34 | args = [] 35 | } 36 | 37 | return function debounced () { 38 | context = this 39 | args.push([].slice.call(arguments)) 40 | timestamp = now() 41 | var callNow = immediate && !timer 42 | if (!timer) timer = setTimeout(onTimeout, wait) 43 | if (callNow) { 44 | result = call() 45 | reset() 46 | } 47 | 48 | return result 49 | } 50 | } 51 | 52 | return debounce 53 | })); // eslint-disable-line semi 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # debounce-collect 2 | 3 | An implementation of debounce. Unlike [underscore]'s debounce, this one 4 | collects arguments of all calls. 5 | 6 | This is based on underscore's [implementation](https://github.com/jashkenas/underscore/blob/1.8.3/underscore.js#L813-L847) of debounce. 7 | 8 | [![Status](https://travis-ci.org/rstacruz/debounce-collect.svg?branch=master)](https://travis-ci.org/rstacruz/debounce-collect "See test builds") 9 | 10 | [underscore]: http://underscorejs.org/ 11 | 12 | ## Usage 13 | 14 | Debounce-collect is available as an npm package. 15 | 16 | ```js 17 | var debounce = require('debounce-collect') 18 | ``` 19 | 20 | When used outside a CommonJS environment, it's accessible as `global.debounceCollect`. 21 | 22 | ## Description 23 | 24 | ```js 25 | debounce(function, wait, [immediate]) 26 | ``` 27 | 28 | Creates and returns a new debounced version of the passed function which will 29 | postpone its execution until after `wait` milliseconds have elapsed since the 30 | last time it was invoked. Useful for implementing behavior that should only 31 | happen after the input has stopped arriving. For example: rendering a preview 32 | of a Markdown comment, recalculating a layout after the window has stopped 33 | being resized, and so on. 34 | 35 | Pass *true* for the `immediate` argument to cause `debounce()` to trigger the function 36 | on the leading instead of the trailing edge of the `wait` interval. Useful in 37 | circumstances like preventing accidental double-clicks on a "submit" button 38 | from firing a second time. 39 | 40 | ### Differences from `_.debounce` 41 | 42 | The resulting function will be called with an array of all function arguments 43 | for all calls. 44 | 45 | ## Example 46 | 47 | ```js 48 | // this will call the update function: 49 | // 50 | // update('change', 'file1.txt') 51 | // update('change', 'file2.txt') 52 | // 53 | chokidar.watch('.') 54 | .on('all', debounce(update, 50)) 55 | 56 | function update (argsList) { 57 | console.log(argsList) 58 | 59 | // result: 60 | // [ 61 | // [ 'change', 'file1.txt' ], 62 | // [ 'change', 'file2.txt' ] 63 | // ] 64 | } 65 | ``` 66 | 67 | ## Thanks 68 | 69 | **debounce-collect** © 2015+, Rico Sta. Cruz. Released under the [MIT] License.
70 | Authored and maintained by Rico Sta. Cruz with help from contributors ([list][contributors]). 71 | 72 | > [ricostacruz.com](http://ricostacruz.com)  ·  73 | > GitHub [@rstacruz](https://github.com/rstacruz)  ·  74 | > Twitter [@rstacruz](https://twitter.com/rstacruz) 75 | 76 | [MIT]: http://mit-license.org/ 77 | [contributors]: http://github.com/rstacruz/debounce-collect/contributors 78 | --------------------------------------------------------------------------------