├── LICENSE.md ├── README.md ├── index.js └── package.json /LICENSE.md: -------------------------------------------------------------------------------- 1 | This software is released under the MIT license: 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # frame-debounce [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 2 | 3 | Decorate a function so that it only fires once per frame, using 4 | `requestAnimationFrame` in the browser and `setImmediate` or `process.nextTick` 5 | in node. 6 | 7 | ## Usage 8 | 9 | [![NPM](https://nodei.co/npm/frame-debounce.png)](https://nodei.co/npm/frame-debounce/) 10 | 11 | ### debounced = debounce(fn, [immediate]) 12 | 13 | Returns a decorated version of `fn`, that can only be called once per frame 14 | 15 | ## See Also 16 | 17 | * [debounce](http://github.com/component/debounce) 18 | * [frame-queue](https://github.com/hughsk/frame-queue) 19 | 20 | ## License 21 | 22 | MIT. See [LICENSE.md](http://github.com/hughsk/frame-debounce/blob/master/LICENSE.md) for details. 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var slice = require('sliced') 2 | var raf = require('raf') 3 | 4 | module.exports = debounce 5 | 6 | function debounce(fn, now) { 7 | var args = null 8 | var ctx = null 9 | 10 | // I don't know why, but adding this 11 | // comment seems to stop uglify from 12 | // breaking this code. 13 | 14 | return debounced 15 | 16 | function debounced() { 17 | if (args !== null) return 18 | args = slice(arguments) 19 | ctx = this 20 | if (now) fn.apply(ctx, args) 21 | raf(next) 22 | } 23 | 24 | function next() { 25 | if (!now) fn.apply(ctx, args) 26 | args = null 27 | ctx = null 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "frame-debounce", 3 | "version": "1.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/hughsk/frame-debounce.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/hughsk/frame-debounce/issues" 17 | }, 18 | "homepage": "https://github.com/hughsk/frame-debounce", 19 | "dependencies": { 20 | "raf": "^2.0.4", 21 | "sliced": "0.0.5" 22 | } 23 | } 24 | --------------------------------------------------------------------------------