├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── basic.js /.npmignore: -------------------------------------------------------------------------------- 1 | .travis.yml 2 | test/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Feross Aboukhadijeh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # function-with-timeout [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] 2 | 3 | [travis-image]: https://img.shields.io/travis/feross/function-with-timeout/master.svg 4 | [travis-url]: https://travis-ci.org/feross/function-with-timeout 5 | [npm-image]: https://img.shields.io/npm/v/function-with-timeout.svg 6 | [npm-url]: https://npmjs.org/package/function-with-timeout 7 | [downloads-image]: https://img.shields.io/npm/dm/function-with-timeout.svg 8 | [downloads-url]: https://npmjs.org/package/function-with-timeout 9 | [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg 10 | [standard-url]: https://standardjs.com 11 | 12 | ### Ensure a function is always called within a timeout period 13 | 14 | Accepts a function as input and returns a new function. If the returned function is 15 | called before the timeout period (the default timeout is one second), it clears 16 | the timeout and invokes the input function. If the returned function isn't called 17 | before the timeout period, the input function is called regardless. 18 | 19 | Works in the browser, with [browserify](http://browserify.org/)! Module development sponsored by [Study Notes](https://www.apstudynotes.org). 20 | 21 | ## install 22 | 23 | ``` 24 | npm install function-with-timeout 25 | ``` 26 | 27 | ## usage 28 | 29 | Say you're using Google Analytics and you want to send a beacon when a form is 30 | submitted. The problem is that in most browsers, XHR requests are canceled when the 31 | page is unloaded, which will happen on form submission. So, we want to block the 32 | page navigation to give time for the beacon to be sent. 33 | 34 | But we also don't want the form submit to fail if the Analytics server never responds 35 | or the library fails to load. 36 | 37 | Here's what we can do: 38 | 39 | ```js 40 | var functionWithTimeout = require('function-with-timeout') 41 | 42 | form.addEventListener('submit', function (event) { 43 | // prevent form submission 44 | event.preventDefault() 45 | 46 | // send analytics beacon 47 | ga('send', 'event', 'Signup Form', 'submit', { 48 | // submit the form when beacon request is sent or 1000ms has elapsed, 49 | // whichever comes first 50 | hitCallback: functionWithTimeout(function () { 51 | form.submit() 52 | }) 53 | }) 54 | }) 55 | ``` 56 | 57 | ## license 58 | 59 | MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). 60 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! function-with-timeout. MIT License. Feross Aboukhadijeh */ 2 | module.exports = function functionWithTimeout (cb, timeout) { 3 | var called = false 4 | var fn = function () { 5 | if (!called) { 6 | called = true 7 | cb() 8 | } 9 | } 10 | setTimeout(fn, timeout || 1000) 11 | return fn 12 | } 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "function-with-timeout", 3 | "description": "Ensure a function is always called within a timeout period", 4 | "version": "1.0.4", 5 | "author": { 6 | "name": "Feross Aboukhadijeh", 7 | "email": "feross@feross.org", 8 | "url": "https://feross.org" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/feross/function-with-timeout/issues" 12 | }, 13 | "dependencies": {}, 14 | "devDependencies": { 15 | "standard": "*", 16 | "tape": "^5.0.1" 17 | }, 18 | "homepage": "https://github.com/feross/function-with-timeout", 19 | "keywords": [ 20 | "function", 21 | "timeout", 22 | "ensure", 23 | "guarantee", 24 | "called", 25 | "timeout function", 26 | "create function", 27 | "create function with timeout", 28 | "function with timeout", 29 | "google analytics" 30 | ], 31 | "license": "MIT", 32 | "main": "index.js", 33 | "repository": { 34 | "type": "git", 35 | "url": "git://github.com/feross/function-with-timeout.git" 36 | }, 37 | "scripts": { 38 | "test": "standard && tape test/*.js" 39 | }, 40 | "funding": [ 41 | { 42 | "type": "github", 43 | "url": "https://github.com/sponsors/feross" 44 | }, 45 | { 46 | "type": "patreon", 47 | "url": "https://www.patreon.com/feross" 48 | }, 49 | { 50 | "type": "consulting", 51 | "url": "https://feross.org/support" 52 | } 53 | ] 54 | } 55 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | var functionWithTimeout = require('../') 2 | var test = require('tape') 3 | 4 | test('function never gets called manually', function (t) { 5 | t.plan(1) 6 | var d = Date.now() 7 | function myFn () { 8 | var elapsed = Date.now() - d 9 | t.ok(elapsed >= 1000) 10 | } 11 | functionWithTimeout(myFn) 12 | }) 13 | 14 | test('function gets called before timeout', function (t) { 15 | t.plan(2) 16 | var d = Date.now() 17 | function myFn () { 18 | var elapsed = Date.now() - d 19 | t.ok(elapsed >= 100) 20 | t.ok(elapsed < 1000) 21 | } 22 | var fn = functionWithTimeout(myFn) 23 | setTimeout(fn, 100) 24 | }) 25 | 26 | test('function gets called after timeout', function (t) { 27 | t.plan(2) 28 | var d = Date.now() 29 | function myFn () { 30 | var elapsed = Date.now() - d 31 | t.ok(elapsed >= 1000) 32 | t.ok(elapsed < 1500) 33 | } 34 | var fn = functionWithTimeout(myFn) 35 | setTimeout(fn, 1500) 36 | }) 37 | 38 | test('function gets called before timeout (custom timeout)', function (t) { 39 | t.plan(2) 40 | var d = Date.now() 41 | function myFn () { 42 | var elapsed = Date.now() - d 43 | t.ok(elapsed >= 100) 44 | t.ok(elapsed < 200) 45 | } 46 | var fn = functionWithTimeout(myFn, 200) 47 | setTimeout(fn, 100) 48 | }) 49 | 50 | test('function gets called after timeout (custom timeout)', function (t) { 51 | t.plan(2) 52 | var d = Date.now() 53 | function myFn () { 54 | var elapsed = Date.now() - d 55 | t.ok(elapsed >= 500) 56 | t.ok(elapsed < 20 * 1000) 57 | } 58 | var fn = functionWithTimeout(myFn, 500) 59 | setTimeout(fn, 1000) 60 | }) 61 | --------------------------------------------------------------------------------