├── LICENSE ├── README.md ├── package.json └── set-timer.js /LICENSE: -------------------------------------------------------------------------------- 1 | Free Public License 1.0.0 2 | 3 | Permission to use, copy, modify, and/or distribute this software for 4 | any purpose with or without fee is hereby granted. 5 | 6 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 7 | WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 8 | OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE 9 | FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY 10 | DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN 11 | AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 12 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | setTimer 2 | ======== 3 | 4 | A lightweight abstraction for the JavaScript Timer API. 5 | 6 | ## Installation 7 | 8 | Install setTimer as an [NPM package](https://www.npmjs.org/package/set-timer). 9 | 10 | ```sh 11 | npm install set-timer 12 | ``` 13 | 14 | ## Usage 15 | 16 | The `setTimer` function takes a callback function and an optional options object as arguments. 17 | 18 | ```javascript 19 | var setTimer = require('set-timer'); 20 | 21 | setTimer(function() { 22 | console.log("I'm called immediately!"); 23 | }); 24 | ``` 25 | 26 | The options are: 27 | 28 | * **Timeout:** Number of milliseconds to wait before the first call (default: `0`). 29 | * **Limit:** Number of times to call callback (default: `1`). Use `Infinity` to call indefinitely. 30 | * **Interval:** Number of milliseconds to wait between calls (default: `0`). Ignored unless `limit > 1`. 31 | * **onClear:** Function to call after timer has been cleared (default: `Function.prototype`). 32 | 33 | ```javascript 34 | var setTimer = require('set-timer'); 35 | 36 | var timer = setTimer(function () { 37 | console.log("I've been called " + this.calls + " times!"); 38 | }, { 39 | timeout: 5000, // Wait 5 seconds before first call. 40 | limit: 10, // Call callback 10 times. 41 | interval: 1000, // Wait 1 second between calls. 42 | onClear: function () { // Call after timer is cleared. 43 | console.log("Cleared!"); 44 | } 45 | }); 46 | ``` 47 | 48 | Timers are automatically cleared after their call count reaches `options.limit`, but you can clear the timer manually `this.clear()` from inside of scope the callback or `timer.clear()` in the scope of the timer. 49 | 50 | ## Support 51 | 52 | Please [open an issue](https://github.com/fraction/set-timer/issues/new) for questions and concerns. 53 | 54 | ## Contributing 55 | 56 | Fork the project, commit your changes, and [open a pull request](https://github.com/fraction/set-timer/compare/). 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "set-timer", 3 | "version": "2.1.4", 4 | "description": "A lightweight abstraction for the JavaScript Timer API.", 5 | "main": "set-timer.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/fraction/set-timer.git" 12 | }, 13 | "keywords": [ 14 | "timer", 15 | "timers", 16 | "timeout", 17 | "interval", 18 | "setTimeout", 19 | "setInterval", 20 | "clearTimeout", 21 | "clearInterval", 22 | "time" 23 | ], 24 | "author": "Christian Bundy (http://christianbundy.com/)", 25 | "license": "FPL", 26 | "bugs": { 27 | "url": "https://github.com/fraction/set-timer/issues" 28 | }, 29 | "homepage": "https://github.com/fraction/set-timer" 30 | } 31 | -------------------------------------------------------------------------------- /set-timer.js: -------------------------------------------------------------------------------- 1 | var setTimer = function (cb, options) { 2 | options = options || Object.prototype; 3 | options.timeout = options.timeout || 0; 4 | options.interval = options.interval || 0; 5 | options.limit = options.limit || 1; 6 | options.onClear = options.onClear || Function.prototype; 7 | options.cb = cb || Function.prototype; 8 | 9 | var timer = { 10 | calls: 0, 11 | options: options, 12 | timeout: null, 13 | interval: null, 14 | clear: function () { 15 | if (this.timeout) clearTimeout(this.timeout); 16 | if (this.interval) clearInterval(this.interval); 17 | options.onClear.call(this); 18 | } 19 | }; 20 | 21 | timer.timeout = setTimeout(function () { 22 | timer.interval = setInterval(function() { 23 | timer.calls++; 24 | options.cb.call(timer); 25 | if (timer.calls >= options.limit) { 26 | timer.clear(); 27 | } 28 | }, options.interval); 29 | }, options.timeout); 30 | 31 | return timer; 32 | }; 33 | 34 | module.exports = setTimer; 35 | --------------------------------------------------------------------------------