{"id":17399241,"url":"https://github.com/feross/function-with-timeout","last_synced_at":"2025-09-13T00:32:42.830Z","repository":{"id":56653266,"uuid":"43654051","full_name":"feross/function-with-timeout","owner":"feross","description":"Ensure a function is always called within a timeout period","archived":false,"fork":false,"pushed_at":"2020-10-27T04:06:06.000Z","size":17,"stargazers_count":19,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-03T02:53:09.831Z","etag":null,"topics":["function","javascript","nodejs","timeout"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/feross.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-04T21:46:41.000Z","updated_at":"2024-09-25T09:10:52.000Z","dependencies_parsed_at":"2022-08-15T22:40:30.020Z","dependency_job_id":null,"html_url":"https://github.com/feross/function-with-timeout","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Ffunction-with-timeout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Ffunction-with-timeout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Ffunction-with-timeout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/feross%2Ffunction-with-timeout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/feross","download_url":"https://codeload.github.com/feross/function-with-timeout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232802624,"owners_count":18578695,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["function","javascript","nodejs","timeout"],"created_at":"2024-10-16T15:13:55.274Z","updated_at":"2025-01-07T00:20:28.629Z","avatar_url":"https://github.com/feross.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 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]\n\n[travis-image]: https://img.shields.io/travis/feross/function-with-timeout/master.svg\n[travis-url]: https://travis-ci.org/feross/function-with-timeout\n[npm-image]: https://img.shields.io/npm/v/function-with-timeout.svg\n[npm-url]: https://npmjs.org/package/function-with-timeout\n[downloads-image]: https://img.shields.io/npm/dm/function-with-timeout.svg\n[downloads-url]: https://npmjs.org/package/function-with-timeout\n[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg\n[standard-url]: https://standardjs.com\n\n### Ensure a function is always called within a timeout period\n\nAccepts a function as input and returns a new function. If the returned function is\ncalled before the timeout period (the default timeout is one second), it clears\nthe timeout and invokes the input function. If the returned function isn't called\nbefore the timeout period, the input function is called regardless.\n\nWorks in the browser, with [browserify](http://browserify.org/)! Module development sponsored by [Study Notes](https://www.apstudynotes.org).\n\n## install\n\n```\nnpm install function-with-timeout\n```\n\n## usage\n\nSay you're using Google Analytics and you want to send a beacon when a form is\nsubmitted. The problem is that in most browsers, XHR requests are canceled when the\npage is unloaded, which will happen on form submission. So, we want to block the\npage navigation to give time for the beacon to be sent.\n\nBut we also don't want the form submit to fail if the Analytics server never responds\nor the library fails to load.\n\nHere's what we can do:\n\n```js\nvar functionWithTimeout = require('function-with-timeout')\n\nform.addEventListener('submit', function (event) {\n  // prevent form submission\n  event.preventDefault()\n\n  // send analytics beacon\n  ga('send', 'event', 'Signup Form', 'submit', {\n    // submit the form when beacon request is sent or 1000ms has elapsed,\n    // whichever comes first\n    hitCallback: functionWithTimeout(function () {\n      form.submit()\n    })\n  })\n})\n```\n\n## license\n\nMIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeross%2Ffunction-with-timeout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeross%2Ffunction-with-timeout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeross%2Ffunction-with-timeout/lists"}